为什么在GoogleAuthentication之后User.Identity.IsAuthenticated不正确?

时间:2015-08-18 03:00:47

标签: oauth oauth-2.0 google-oauth owin katana

我正在尝试使用OWIN和GoogleAuthentication调试问题,所有内容都可以重定向到谷歌,然后谷歌重定向回来,我的自定义GoogleAuthProvider被调用,然后最终重定向发回到所需的页面。

不幸的是,在重定向回到所需的网页后,!User.Identity.IsAuthenticated false ,因此我的帐户控制人员认为需要重定向回谷歌进行身份验证。 调用我的GoogleAuthProvider.Authenticated(GoogleOAuth2AuthenticatedContext context)函数,如果在重定向之前看到当前用户已经过身份验证。

什么可能导致用户在重定向上变得未经身份验证? 为什么应用程序设置或读取请求之间的身份验证值?

这是我的GoogleAuthProvider

public class GoogleAuthProvider : IGoogleOAuth2AuthenticationProvider
    {
        public void ApplyRedirect(GoogleOAuth2ApplyRedirectContext context)
        {
            context.Response.Redirect(context.RedirectUri);
        }

        public Task Authenticated(GoogleOAuth2AuthenticatedContext context)
        {
            context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
            return Task.FromResult<object>(null);
        }

        public Task ReturnEndpoint(GoogleOAuth2ReturnEndpointContext context)
        {
            return Task.FromResult<object>(null);
        }
    }

这是我的ConfigureOAuth

public void ConfigureOAuth(IAppBuilder app)
        {
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
            OAuthBearerOptions = new OAuthBearerAuthenticationOptions();

            // Token Generation
            app.UseOAuthAuthorizationServer(OAuthOptions);
            app.UseOAuthBearerAuthentication(OAuthBearerOptions);

            //Configure Google External Login
            GoogleAuthOptions = new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "x",
                ClientSecret = "x",
                Provider = new GoogleAuthProvider()
            };
            app.UseGoogleAuthentication(GoogleAuthOptions);
}

1 个答案:

答案 0 :(得分:1)

你所有的代码似乎都很好 但您要做的是启用“Google+ API”。 您可以从这里获得更多帮助 http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

如何启用 在开发人员控制台dashboard上查看“启用API”。点击并在搜索类型Google+ API中

您可以在这里启用。

希望这有助于enter image description here