我有一个 ASP.NET MVC 4 应用程序,允许用户使用 Gmail 等外部服务登录。
到目前为止,用户可以登录并在应用程序内导航。但问题出在注销上。我有一个注销按钮,该按钮请求在我的 AccountController
内调用 Controller 操作 LogOff()
。在该方法中,如果用户通过 oauth 进行身份验证,我如何注销?
对于本地帐户,我使用:
public ActionResult LogOff()
{
WebSecurity.Logout();
return RedirectToAction("Login", "Account");
}
但是对于 oauth 我没有看到任何类似的东西...... 我想我需要清除某种 cookie,但我不知道如何...
最佳答案
基于this ,我实现了以下客户端解决方案(我之前询问用户是否也想在提供程序中注销):
//get accountType, accessToken, redirectUrl and clientID
var accountType = ...;
var accessToken = ...;
var redirectUrl = ...;
var clientID = ...;
$("#logoutConfirmButton").on('click', function () {
externalLogout();
});
function externalLogout() {
var url, params;
if (accountType== "facebook") {
url = "https://www.facebook.com/logout.php";
params = {
next: redirectUrl,
access_token: encodeURIComponent(accessToken)
};
performCallLogout(url, params, accountType);
} else if (accountType== "google") {
url = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout";
params = {
next: redirectUrl
}
performCallLogout(url, params, accountType);
} else if (accountType == "microsoft") {
url = "https://login.live.com/oauth20_logout.srf";
params = {
clientId: clientID,
redirectUrl: redirectUrl
}
performCallLogout(url, params, accountType);
}
}
function performCallLogout(url, params, accountType) {
if (accountType == "facebook") {
window.location.href = url + "?next=" + params.next + "&access_token=" + params.access_token;
} else if (accountType == "google") {
window.location.href = url + "?continue=" + params.next;
} else if (accountType == "microsoft") {
window.location.href = url + "?client_id=" + params.clientId + "&redirect_url=" + params.redirectUrl;
}
}
希望这对某人有帮助。
关于asp.net-mvc-4 - 使用 oauth 从外部登录服务(Gmail、facebook)注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946798/