将Google OAuth2身份验证添加到自定义的OWIN中间件

时间:2014-12-12 11:43:17

标签: asp.net facebook authentication google-oauth owin

到目前为止,我一直在使用自定义的OWIN中间件进行WEB API OAuth2身份验证。这适用于内部用户,但不支持Facebook或Google等外部帐户。

所以,我决定尝试添加该功能。

我为Google身份验证管道创建了一个提供程序:

public class GoogleAuthProvider : IGoogleOAuth2AuthenticationProvider {

     public void ApplyRedirect(GoogleOAuth2ApplyRedirectContext context) {
         context.Response.Redirect(context.RedirectUri);
     }

     public Task Authenticated(GoogleOAuth2AuthenticatedContext context) {
         /* I'm creating a new claim so I can retrieve the access token later on from the context */
         Claim authClaim = new Claim(ClaimTypes.Authentication, context.AccessToken, ClaimValueTypes.String);
         context.Identity.AddClaim(authClaim);

         return Task.FromResult<object>(null);
     }

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

我正在配置我的Google身份验证管道,如下所示:

private GoogleAuth2AuthenticationOptions ConfigureGoogle() { 
     return new GoogleOAuth2AuthenticationOptions() {
          ClientId = "xxx",
          ClientSecret = "xxx",
          Provider = new GoogleAuthProvider()
     }
}

然后我将它注入我的应用程序:

GoogleOAuth2AuthenticatioOptions googleConfig = ConfigureGoogle();
application.UseGoogleAuthentication(googleConfig);

在外部登录之前,我一直在为本地帐户使用OAuth承载身份验证。我配置了授权服务器:

OAuthAuthorizationServerOptions serverConfig = new OAuthAuthorizationServerOptions() {
     AllowInsecureHttp = !secure,
     TokenEndpointPath = new PathString(tokenEndpoint),
     AccessTokenExpireTimeSpan = accessTokenExpiration,
     Provider = authorizationProvider, // (OAuthAuthorizationServerProvider)
     RefreshTokenProvider = refreshTokenProvider // (IAuthenticationTokenProvider)
}
application.UseOAuthAuthorizationServer(serverConfig);

我想保留我的访问令牌端点,而不是创建另外一个用于处理外部登录的端点。我有配置的提供商,如何在我的授权服务器上使用它们?

我的授权服务器使用grant_type = password,它需要用户名和密码。我的系统上是否有外部帐户没有密码的授权类型?

0 个答案:

没有答案