1

私は働くアイデンティティ・プロバイダを持っています。私がそれに対して認証するように設計したクライアントは、MVCとWeb APIを組み合わせた単一のプロジェクトです。最初の認証はMVCで行われます。アクセストークンが無効になると、期待どおりに更新されます。mvcとapiがアクセス・トークンをリフレッシュしていないIdentity Server 3クライアント

MVC側:

公共部分クラス起動{

public void ConfigureAuth(IAppBuilder app) 
    { 
     //AntiForgeryConfig.UniqueClaimTypeIdentifier = "sub"; 
     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "Cookies", 
      CookieName = "CookieName", 
      ReturnUrlParameter = "/Dashboard", 
      LogoutPath = new PathString("/"), 
     }); 
app.UseOpenIdConnectAuthentication(GetOpenIdConnectAuthenticationOptions()); 

    } 


    private OpenIdConnectAuthenticationOptions GetOpenIdConnectAuthenticationOptions() 
    { 
     var options = new OpenIdConnectAuthenticationOptions 
     { 
      ClientId = "client.id", 
      Authority = AuthorityUrl, 
      RedirectUri = RedirectUri, 
      PostLogoutRedirectUri = RedirectUri, 
      ResponseType = "code id_token", 
      Scope = "openid profile email offline_access roles company utc_offset service_api", 

      TokenValidationParameters = new TokenValidationParameters 
      { 
       NameClaimType = "name", 
       RoleClaimType = "role" 
      }, 

      SignInAsAuthenticationType = "Cookies", 
      Notifications = GetOpenIdConnectAuthenticationNotifications() 
     }; 
     return options; 
    } 

    private OpenIdConnectAuthenticationNotifications GetOpenIdConnectAuthenticationNotifications() 
    { 
     var container = UnityLazyInit.Container; 
     var authorizationProvider = container.Resolve<AuthorizationProvider>(); 
     var notifications = new OpenIdConnectAuthenticationNotifications 
     { 
      AuthorizationCodeReceived = async n => 
      { 
       authorizationProvider.Authority = Authority; 
       authorizationProvider.LoginMethod = LoginMethod; 
       var tokenResponse = await authorizationProvider.GetAccessAndRefreshTokens(n); 

       var userInfoClaims = await authorizationProvider.GetUserInfoClaims(tokenResponse); 

       userInfoClaims = authorizationProvider.TransformUserInfoClaims(userInfoClaims); 

       // create new identity 
       var id = new ClaimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType); 
       id.AddClaims(userInfoClaims); 

       id.AddClaim(new Claim("access_token", tokenResponse.AccessToken)); 
       id.AddClaim(new Claim("expires_at", DateTime.Now.AddSeconds(tokenResponse.ExpiresIn).ToLocalTime().ToString())); 
       id.AddClaim(new Claim("refresh_token", tokenResponse.RefreshToken)); 
       id.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken)); 
       id.AddClaim(new Claim("sid", n.AuthenticationTicket.Identity.FindFirst("sid").Value)); 

       var user = authorizationProvider.GetUser(id); 

       var applicationClaims = authorizationProvider.GetApplicationClaims(user); 
       id.AddClaims(applicationClaims); 

       var permisionClaims = authorizationProvider.GetPermisionClaims(user); 
       id.AddClaims(permisionClaims); 

       n.AuthenticationTicket = new AuthenticationTicket(
        new ClaimsIdentity(id.Claims, n.AuthenticationTicket.Identity.AuthenticationType, "name", "role"), 
        n.AuthenticationTicket.Properties); 
      }, 

      RedirectToIdentityProvider = n => 
      { 
       // if signing out, add the id_token_hint 
       if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest) 
       { 
        var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token"); 

        if (idTokenHint != null) 
        { 
         n.ProtocolMessage.IdTokenHint = idTokenHint.Value; 
        } 
       } 

       return Task.FromResult(0); 
      } 

     }; 

     return notifications; 
    } 

} 

プレゼンテーション層(ブラウザ側)Iは、認証のための任意のサポートが組み込まれていないがangulerjsを活用します。私はMVCに頼っています。

プレゼンテーションレイヤーがAPIを呼び出すと、MVCによって取得されたアクセストークンに対して自動的に検証されますが、期限が切れた場合はアクセストークンを更新できません。また、無許可では返されません。リフレッシュしようとしているように見えますが失敗します。プレゼンテーションは、api呼び出しがトークンをリフレッシュしようとすると、アイデンティティプロバイダのエラーページのHTMLを受け取ります。

これを修正するにはどうすればよいですか?私は、MVCとAPIが組み合わされたときに自動的に認証とリフレッシュが行われるはずですが、これは私のためには機能しません。

上記の起動設定を明確にするために、MVCとAPIは共有されていますが、

  new Client 
      { 
       ClientName = "MVC Client", 
       ClientId = "client.id", 
       ClientSecrets = new List<Secret> { 
        new Secret("secret".Sha256()) 
       }, 
       Flow = Flows.Hybrid, 

       AllowedScopes = new List<string> 

       { 

        Constants.StandardScopes.OpenId, 

        Constants.StandardScopes.Profile, 

        Constants.StandardScopes.Email, 

        Constants.StandardScopes.OfflineAccess, 

        "roles", 

        "company", 

        "utc_offset", 

        "service_api 

」 }、

   RequireConsent = false, 


       RedirectUris = new List<string> 

       { 

        REMOVED 
       }, 


       PostLogoutRedirectUris = new List<string> 
       { 
        REMOVED 
       }, 


       AllowedCorsOrigins = new List<string> 
       { 
        REMOVED 
       }, 

       AccessTokenLifetime = 60, 
       IdentityTokenLifetime = 60, 
       AbsoluteRefreshTokenLifetime = 60 * 60 * 24, 
       SlidingRefreshTokenLifetime = 60 * 15, 
      }, 

@brockallen - 。この短いが、私はMVCとWEBAPIとAnjulgarjsあるアプリケーションを持っている私はこのようなハイブリッドは賢明ではないと思いますけど。私は、このアプリケーションを継承し、今私はそれがIdnetityサーバで動作するようにする方法を見つけなければならない3.

私は。すべての指導に感謝してくださいだろう。

答えて

1

私はあなたと同じ問題を抱えていた。問題は、MVCのCookieを設定しているにも関わらず、有効期限を設定していないため、MVCはCookieの有効期限が切れていることを認識しません。あなたは、次のようにAuthenticationTicketの有効期限を設定して行うために必要なもの:私はまた、発行日を設定していますが、それは必須ではありません

    n.AuthenticationTicket.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddSeconds(int.Parse(n.ProtocolMessage.ExpiresIn)); 
        n.AuthenticationTicket.Properties.IssuedUtc = DateTime.Now; 
        //Add encrypted MVC auth cookie 
        n.AuthenticationTicket = new AuthenticationTicket(
         nid, 
         n.AuthenticationTicket.Properties); 

関連する問題