2017-05-25 3 views
1

私はIdentityServer 4と.netコアを使用しています。私はWeb APIと、API上の安全なエンドポイントにアクセスするMVCアプリケーションを持っています。それはIdentityServerクイックスタートに設定して非常に似ています:IdentityServer 4で 'refresh_token'を使用するには?

https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/6_AspNetIdentity

私は私のaccess_tokensが期限切れしていることを発見しています、と私はrefresh_tokensを再交渉する方法を理解したいと思います。

(クイックスタートhereから採取した)は、例えば、次のコードを取る:access_tokenが満了した

public async Task<IActionResult> CallApiUsingUserAccessToken() 
    { 
     var accessToken = await HttpContext.Authentication.GetTokenAsync("access_token"); 

     var client = new HttpClient(); 
     client.SetBearerToken(accessToken); 
     var content = await client.GetStringAsync("http://localhost:5001/identity"); 

     ViewBag.Json = JArray.Parse(content).ToString(); 
     return View("json"); 
    } 

場合、それは401応答で失敗します。 refresh_tokenを使用してaccess_tokenを再交渉するための組み込みのメカニズムはありますか?

答えて

2

access_tokenを更新するシステムはありません。ただし、IdentityModelパッケージを使用して、refresh_tokenの新しいaccess_tokenをリクエストすることができます。

ClientのプロパティはAllowOfflineAccessで、IdentityServerではtrueに設定する必要があります。これは、ではなく、暗黙的/クライアントの資格情報フローの場合はで動作することに注意してください。現在access_token

  • その寿命をチェックしrefresh_token(個人的な好み)で新しいaccess_tokenを要求することにより、期限切れになる場合は、必ずaccess_tokenは前に保護されたリソース
  • チェックするために電話をかけるにリフレッシュ

    • あなたがを確認することができます前、このコードにrefresh_token

    で新しいaccess_token 401広告リクエストを返すためにAPIを待ち寿命および/または新しいaccess_token

    var discoveryResponse = await DiscoveryClient.GetAsync("IdentityServer url"); 
    if (discoveryResponse.IsError) 
    { 
        throw new Exception(discoveryResponse.Error); 
    } 
    
    var tokenClient = new TokenClient(discoveryResponse.TokenEndpoint, "ClientId", "ClientSecret"); 
    // This will request a new access_token and a new refresh token. 
    var tokenResponse = await tokenClient.RequestRefreshTokenAsync(await httpContext.Authentication.GetTokenAsync("refresh_token")); 
    
    if (tokenResponse.IsError) 
    { 
        // Handle error. 
    } 
    
    var oldIdToken = await httpContext.Authentication.GetTokenAsync("id_token"); 
    
    var tokens = new List<AuthenticationToken> 
    { 
        new AuthenticationToken 
        { 
         Name = OpenIdConnectParameterNames.IdToken, 
         Value = oldIdToken 
        }, 
        new AuthenticationToken 
        { 
         Name = OpenIdConnectParameterNames.AccessToken, 
         Value = tokenResult.AccessToken 
        }, 
        new AuthenticationToken 
        { 
         Name = OpenIdConnectParameterNames.RefreshToken, 
         Value = tokenResult.RefreshToken 
        } 
    }; 
    
    var expiresAt = DateTime.UtcNow.AddSeconds(tokenResult.ExpiresIn); 
    tokens.Add(new AuthenticationToken 
    { 
        Name = "expires_at", 
        Value = expiresAt.ToString("o", CultureInfo.InvariantCulture) 
    }); 
    
    // Sign in the user with a new refresh_token and new access_token. 
    var info = await httpContext.Authentication.GetAuthenticateInfoAsync("Cookies"); 
    info.Properties.StoreTokens(tokens); 
    await httpContext.Authentication.SignInAsync("Cookies", info.Principal, info.Properties); 
    
    を要求する前に、サービスでこのコードをラップ

    から取られ、わずかに変更された:Source

  • +0

    グレート。ありがとう。これは機能します。 –

    関連する問題