在我的应用程序中,所有的身份验证都是通过 Google 进行的 - 即所有用户都是 Google 帐户。我不需要用户在我的应用程序中进行注册,只需使用 Google 帐户登录即可。然而,我希望使用 ASP.net Identity(我想)来管理用户的角色。考虑到这一点,在外部身份验证成功后,我会创建一个 ASP.net Identity 用户(如果不存在)。所以,我的 ExternalLoginCallback 如下所示:
想法是我现在可以管理用户、添加角色、检查用户是否处于角色中等等……
首先,这种方法可行吗?或者我过于复杂了?
然而,我遇到的一个问题是如何退出我的应用程序。
我的“Logout”操作如下:
[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");
}
我的Index操作被[Authorize]属性装饰 - 但是,当我“注销”时 - 它会重定向到Home.Index - 但我似乎仍然登录着?