为什么在 GoogleAuthentication 之后 User.Identity.IsAuthenticated 不正确?
Why is User.Identity.IsAuthenticated not true after GoogleAuthentication?
我正在尝试调试 OWIN 和 GoogleAuthentication 的问题,重定向到 google 后一切正常,然后 google 重定向回来,我的自定义 GoogleAuthProvider 被调用,最后发生重定向回到想要的页面。
不幸的是,在重定向回所需页面后,!User.Identity.IsAuthenticated
为 false 因此我的帐户控制器假设它需要重定向回 google验证。
我的 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);
}
你所有的代码看起来都不错
但是你要做的是启用 "Google+ API."
您可以从这里获得更多帮助
http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on
如何启用
在开发人员控制台仪表板上查找 "Enable API"。单击并在搜索中键入 Google+ API
您可以在这里启用。
希望对您有所帮助
我正在尝试调试 OWIN 和 GoogleAuthentication 的问题,重定向到 google 后一切正常,然后 google 重定向回来,我的自定义 GoogleAuthProvider 被调用,最后发生重定向回到想要的页面。
不幸的是,在重定向回所需页面后,!User.Identity.IsAuthenticated
为 false 因此我的帐户控制器假设它需要重定向回 google验证。
我的 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);
}
你所有的代码看起来都不错 但是你要做的是启用 "Google+ API." 您可以从这里获得更多帮助 http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on
如何启用 在开发人员控制台仪表板上查找 "Enable API"。单击并在搜索中键入 Google+ API
您可以在这里启用。
希望对您有所帮助