2017-01-30 7 views
2

私はasp.netアイデンティティでパスワード許可フローを使用しています。ユーザーのすべてのリフレッシュトークンを破棄

ログインが実行されるたびに、ユーザーのすべてのリフレッシュトークンを強制終了します。 他のPCやスマートフォンなどの別のデバイスでサインインしても、「セッション」を終了する必要があります。

どうすればいいですか?

私はちょうどUserManager.UpdateSecurityStampAsync(user.Id);を行うことができますか、それとも別のものが必要ですか?

ありがとうございました!

答えて

2

私はちょうどUserManager.UpdateSecurityStampAsync(user.Id);を行うことができますか?

これは間違いありません。そのためには、トークンエンドポイントを調整して、有効なトークンレスポンスを返す前にIdentityにセキュリティスタンプの検証を依頼してください。ここでは例です:

また
[HttpPost("~/connect/token"), Produces("application/json")] 
public async Task<IActionResult> Exchange(OpenIdConnectRequest request) { 
    // ... 

    if (request.IsRefreshTokenGrantType()) { 
     // Retrieve the claims principal stored in the refresh token. 
     var info = await HttpContext.Authentication.GetAuthenticateInfoAsync(
      OpenIdConnectServerDefaults.AuthenticationScheme); 

     // Retrieve the user profile and validate the 
     // security stamp stored in the refresh token. 
     var user = await _signInManager.ValidateSecurityStampAsync(info.Principal); 
     if (user == null) { 
      return BadRequest(new OpenIdConnectResponse { 
       Error = OpenIdConnectConstants.Errors.InvalidGrant, 
       ErrorDescription = "The refresh token is no longer valid." 
      }); 
     } 

     // Ensure the user is still allowed to sign in. 
     if (!await _signInManager.CanSignInAsync(user)) { 
      return BadRequest(new OpenIdConnectResponse { 
       Error = OpenIdConnectConstants.Errors.InvalidGrant, 
       ErrorDescription = "The user is no longer allowed to sign in." 
      }); 
     } 

     // Create a new authentication ticket, but reuse the properties stored 
     // in the refresh token, including the scopes originally granted. 
     var ticket = await CreateTicketAsync(request, user, info.Properties); 

     return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme); 
    } 

    // ... 
} 

、あなたはまた、ユーザーに関連付けられているすべてのリフレッシュトークンを取り消すOpenIddictTokenManagerを使用することができます。

foreach (var token in await manager.FindBySubjectAsync("[userid]", cancellationToken)) { 
    await manager.RevokeAsync(token, cancellationToken); 
} 
+0

それは」を解決...しかし、私のコードは少し異なっていますなぜ、あなたは 'request.IsRefreshTokenGrantType()'について言及しましたが、 'OpenIddictTokenManager'を使って' request.IsPasswordGrantType() 'に私のコードを入れました。なぜなら、私がすべてのトークンをきれいにしなければならないと思うからです。ユーザーは新しいログインを行います。それは正しいのですか、それとも間違った方法でやっていますか? – Jedi31

+0

最初のスニペットは、リフレッシュトークンを取り消しませんが、セキュリティスタンプが変更されたために無効になっているとみなした場合です。あなたが望むものを実現するために、2番目のスニペットを実際にパスワードトークン処理コードに入れることができます。 – Pinpoint

+0

ありがとう! – Jedi31

関連する問題