ASP.net身份验证 - 外部登录 - 无法注销

3
在我的应用程序中,所有的身份验证都是通过 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");
}

我的Index操作被[Authorize]属性装饰 - 但是,当我“注销”时 - 它会重定向到Home.Index - 但我似乎仍然登录着?

2个回答

2

那就是——我实际上并不想退出Google,只是退出我的应用程序... - Alex
嗯...一旦您执行注销操作,cookie 是否被删除? - Brendan Green

1

完成帖子 带有返回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]");
    }

不是的。这会完全注销用户的 Google 帐户,而不是退出应用程序。 - Joep Beusenberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接