简体   繁体   English

Google身份验证OWIN ASP.NET

[英]Google Authentication OWIN ASP.NET

I have the following code that checks to authenticate users using the google. 我有以下代码检查使用谷歌验证用户。 I've tried 3 ways to do this (the 2 signin() & setting the user property). 我已经尝试了3种方法(2登录()和设置用户属性)。 The signin seems to not be doing anything (the user property does not change at all) and even the method for setting the user property, it seems to not remember the change and I end up in an infinite redirect loop and I can't figure out why. 登录似乎没有做任何事情(用户属性根本没有改变)甚至设置用户属性的方法,它似乎不记得改变,我最终在无限重定向循环,我无法想象为什么。

PS: the routing & flow of code does work, its just that when I do the redirect to "home index", authentication is not recognized and I get sent back into this logic. PS:代码的路由和流程确实有效,只是当我重定向到“home index”时,身份验证无法识别,我被送回到这个逻辑中。

FYI: The ChallengeResult class is the helper class that is generated. 仅供参考:ChallengeResult类是生成的辅助类。

    public ActionResult ExternalLogin()
    {
        // Request a redirect to the external login provider
        return new ChallengeResult("Google", Url.Action("ExternalLoginCallback", "GoogleAuth"));
    }

    [Route("ExternalLoginCallback")]
    public async Task<ActionResult> ExternalLoginCallback()
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        var externalIdentity = await HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
        var emailClaim = externalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
        var email = emailClaim.Value;
        if (loginInfo != null && (email.Contains("@mydomain.com") || email.Contains("@mydomain.ca")))
        {
            //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            //AuthenticationManager.SignIn(externalIdentity);
            //AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, externalIdentity);

            AuthenticationManager.User = new ClaimsPrincipal(externalIdentity);

            return RedirectToAction("Index", "Home");
        }
        else
        {
            return new HttpUnauthorizedResult();
        }
    }

    private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

In home controller: 在家庭控制器:

    [Authorize]
    public ActionResult Index()
    {
        return View();
    }

One thing that is important to enable in the google console the Google+ API otherwise the request will return access_denied 有一点很重要,在Google控制台中启用Google+ API,否则请求将返回access_denied

Google控制台

Once you do that if that doesn't work, then you can try to see if this answer helps you 一旦你这样做,如果这不起作用,那么你可以尝试看看这个答案是否有助于你

ASP.NET Identity OWIN Middleware Google OAuth2 AuthenticationManager SignIn not working ASP.NET身份OWIN中间件Google OAuth2 AuthenticationManager SignIn无法正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM