简体   繁体   English

OpenIddict在响应之前获取token_id

[英]OpenIddict get token_id before response

[AllowAnonymous]
[HttpPost("~/api/auth/login")]
[Produces("application/json")]
public async Task<IActionResult> Login(OpenIdConnectRequest request)
{
  ...
      var ticket = await CreateTicketAsync(request, user);
      _logger.LogInformation($"User logged in (id: {user.Id})");

      // Returning a SignInResult will ask OpenIddict to issue the appropriate access/identity tokens.

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

OpenIddict creates token after SignIn method is called right? OpenIddict在调用SignIn方法之后创建令牌吗? How can i access created refresh_token token_id before response is sent? 发送响应之前,如何访问创建的refresh_token token_id?

I want to associate token_id with custom device_id and save to database. 我想将token_id与自定义device_id关联并保存到数据库。 Then i will allow user to revoke refresh_token with specified device. 然后,我将允许用户使用指定的设备撤消refresh_token。

Here's how you can do with the latest RC2 packages (that you can find on the MyGet feed). 这是如何使用最新的RC2程序包(可以在MyGet提要中找到)。

1) Create custom entities derived from the built-in ones: 1)创建从内置实体派生的自定义实体:

public class MyApplication : OpenIddictApplication<string, MyAuthorization, MyToken>
{
    public MyApplication() => Id = Guid.NewGuid().ToString();
}

public class MyAuthorization : OpenIddictAuthorization<string, MyApplication, MyToken>
{
    public MyAuthorization() => Id = Guid.NewGuid().ToString();
}

public class MyScope : OpenIddictScope<string>
{
    public MyScope() => Id = Guid.NewGuid().ToString();
}

public class MyToken : OpenIddictToken<string, MyApplication, MyAuthorization>
{
    public MyToken() => Id = Guid.NewGuid().ToString();

    public string DeviceId { get; set; }
}

2) Update your authorization controller to store the device identifier as an authentication property: 2)更新您的授权控制器,以将设备标识符存储为身份验证属性:

// ...
var ticket = new AuthenticationTicket(principal, properties,
    OpenIdConnectServerDefaults.AuthenticationScheme);

ticket.SetProperty("device_id", "[the device identifier]");
// ...

3) Create a custom token manager and override the PopulateAsync method to attach the device identifier to the token entry stored in the database: 3)创建一个自定义令牌管理器,并重写PopulateAsync方法以将设备标识符附加到存储在数据库中的令牌条目:

public class MyManager : OpenIddictTokenManager<MyToken>
{
    public MyManager(
        IOpenIddictTokenStore<MyToken> store,
        ILogger<OpenIddictTokenManager<MyToken>> logger)
        : base(store, logger)
    {
    }

    protected override Task PopulateAsync(MyToken token, OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken)
    {
        if (descriptor.Properties.TryGetValue("device_id", out var identifier))
        {
            token.DeviceId = identifier;
        }

        return base.PopulateAsync(token, descriptor, cancellationToken);
    }
}

4) Update your Startup class to use the new entities and the custom manager: 4)更新您的Startup类以使用新实体和定制管理器:

services.AddDbContext<ApplicationDbContext>(options =>
{
    // ...
    options.UseOpenIddict<MyApplication, MyAuthorization, MyScope, MyToken, string>();
});

services.AddOpenIddict<MyApplication, MyAuthorization, MyScope, MyToken>(options =>
{
    // ...
    options.AddTokenManager<MyManager>();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM