2016-09-08 4 views
0

私は、OWINを使用するOAuth 2認証プロバイダであるASP.NET Web APIプロジェクトを持っています。 OAuthアクセストークンが期限切れになると、ユーザーにログインさせずにトークンをサイレント更新するために、Cookie認証にフォールバックする必要があります。これを達成する方法はありますか?OWINを使用してWeb APIでOAuthとCookie認証を組み合わせるにはどうすればよいですか?

Googleでは、Angularページからの暗黙的な許可フローを使用してAPIにアクセスしています。私たちは、これがより安全であると感じるので、トークンを定期的に期限切れにしたいと思っています。私たちは、ログインページで発行されたクッキーを使用して、対話型ログインなしでトークンをしばらく発行したいと考えています。

我々はStartup.csでこれを持っている:私たちは、10時間15分とCookieExpirationTimeSpanにAccessTokenExpirationTimeSpanを設定している

public class LegacyAuthorizationServerProvider : OAuthAuthorizationServerProvider 
{ 
    ... 
    private async Task Authorize(OAuthAuthorizeEndpointContext context) 
    { 
     if (context == null) { throw ArgumentIs.Null(nameof(context)); } 

     var authenticateResult = await context.OwinContext.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie); 
     if (authenticateResult?.Identity == null) 
     { 
      context.OwinContext.Authentication.Challenge(DefaultAuthenticationTypes.ApplicationCookie); 
     } 
     else 
     { 
      var claimsIdentity = new ClaimsIdentity(
       authenticateResult.Identity.Claims, 
       "Bearer", 
       "sub", 
       "role"); 

      context.OwinContext.Authentication.SignIn(claimsIdentity); 
     } 

     context.RequestCompleted(); 
    } 

public void Configuration(IAppBuilder app) 
{ 
    var webConfiguration = WebConfiguration.Initialize(WebConfigurationManager.AppSettings); 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     AuthenticationMode = AuthenticationMode.Passive, 
     LoginPath = new PathString("/logon"), 
     ExpireTimeSpan = webConfiguration.Authentication.CookieExpirationTimeSpan 
    }); 

    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 

    app.UseOAuthAuthorizationServer(
     new OAuthAuthorizationServerOptions 
     { 
      AllowInsecureHttp = true, 
      AuthorizeEndpointPath = new PathString("/oauth/authorize"), 
      AccessTokenExpireTimeSpan = webConfiguration.OAuth.AccessTokenExpirationTimeSpan, 
      Provider = new LegacyAuthorizationServerProvider(
       new OAuthClientValidator(new WebConfigurationOAuthClientSource(webConfiguration)), 
       new LegacyAuthenticator(new AuthenticationResultHandlerFactory(), new RefreshTokenResultHandler())), 
     }); 
    var httpConfiguration = new HttpConfiguration(); 

    httpConfiguration.SuppressDefaultHostAuthentication(); 

    httpConfiguration.Filters.Add(new HostAuthenticationAttribute(OAuthDefaults.AuthenticationType)); 

} 

は、その後私たちのLegacyAuthorizationServerProviderに我々はこれを持っていますクッキーが期限切れになるまでログインせずにトークンを発行する意図。問題は、OAuthアクセストークンの有効期限が切れた後にAuthenticateAsync()がnullを返すことです。トークンが期限切れになる前にauthorizeを呼び出すと成功します。

FYI、ここでサインログインページコントローラのコードです:

[HttpPost] 
    [ActionName("logon")] 
    public ActionResult LogOnPost(LogOnFormModel form) 
    { 
     if (form == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest, Resources.AuthenticationController_LogOnPost_NullFormResultMessage); 
     } 

     var context = new LegacyAuthenticationContext(form.UserName, form.Password); 

     _legacyAuthenticator.Authenticate(context); 

     if (context.Authenticated) 
     { 
      var claimsIdentity = new ClaimsIdentity(
       context.CreateClaims(), 
       "sub", 
       "role"); 

      HttpContext.GetOwinContext().Authentication.SignIn(claimsIdentity); 
     } 
     return LogOn(); 
    } 

は、我々は別のOAuthを発行するときに我々は(ユーザーログインを持つの代わりに認証するためにクッキーを使用することができます方法はありますが、トークン)? Cookieの期限が切れると、別のログインが必要になります。

答えて

0

私たちが知ったことは、クッキーの有効期限がある時点で上書きされていたことです。理由はまだわかりません。私たちは、CookieAuthenticationOptions.Provider

の一環としてStartup.Auth.csでこのプロバイダに追加

private class CookieProvider : CookieAuthenticationProvider 
    { 
     public CookieProvider() 
     { 
      OnResponseSignIn = SignIn; 
     } 

     private void SignIn(CookieResponseSignInContext obj) 
     { 
      if (obj.Properties.IssuedUtc.HasValue) 
      { 
       obj.Properties.ExpiresUtc = obj.Properties.IssuedUtc.Value.Add(obj.Options.ExpireTimeSpan); 
      } 
     } 
    } 

:しかし、我々は今、この回避策を使用しています

関連する問題