[英]ASP.net identity - external login - won't log out
在我的应用程序中,我所有的身份验证均通过Google进行,即我的所有用户均为Google帐户。
我不需要用户注册我的应用程序,只需使用Google帐户登录即可。 但是,我确实想使用ASP.net Identity管理用户的角色( 我认为 )
考虑到这一点,在成功进行外部身份验证时,我创建了一个ASP.net Identity用户(如果不存在)
因此,我的ExternalLoginCallback如下所示:
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var authenticationManager = Request.GetOwinContext().Authentication;
var loginInfo = await authenticationManager.GetExternalLoginInfoAsync();
//successfully authenticated with google, so sign them in to our app
var id = new ClaimsIdentity(loginInfo.ExternalIdentity.Claims, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(id);
//Now we need to see if the user exists in our database
var user = UserManager.FindByName(loginInfo.Email);
if (user == null)
{
//user doesn't exist, so the user needs to be created
user = new ApplicationUser { UserName = loginInfo.Email, Email = loginInfo.Email };
await UserManager.CreateAsync(user);
//add the google login to the newly created user
await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
}
return RedirectToLocal(returnUrl);
}
现在,我可以管理用户,添加角色,检查用户是否在角色中,等等。
首先,这是明智的做法吗? 还是我过于复杂了?
但是,我遇到的一个问题是退出应用程序
我的Logout
操作如下所示:
public ActionResult LogOut()
{
HttpContext.GetOwinContext().Authentication.SignOut();
return RedirectToAction("Index", "Home");
}
我的索引动作装饰有[Authorize]属性-但是,当我“注销”时-它重定向到Home.Index-但我似乎仍然可以登录?
根据此ASPNet Identity工作项 ,这是设计使然 ,您需要直接调用Google的API才能注销用户。
使用返回URL(OAuth)完成帖子注销链接,这是对我有用的解决方案:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
return Redirect("https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://[url-of-your-site]");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.