覆盖 AccessTokenExpireTimeSpan [英] Override AccessTokenExpireTimeSpan

查看:15
本文介绍了覆盖 AccessTokenExpireTimeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以覆盖自定义 OAuthAuthorizationServerProvider 上特定票证的默认 AccessTokenExpireTimeSpan?所有其他票证的默认过期时间为 15 分钟.

Is possible to override the default AccessTokenExpireTimeSpan for a specific ticket on a custom OAuthAuthorizationServerProvider? The default expiration time for all other tickets is 15 minutes.

public public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    ...
    var ticket = new AuthenticationTicket(identity, properties);

    if (condition)
    {
        ticket.Properties.IssuedUtc = DateTime.UtcNow;
        ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddDays(14);
    }

    context.Validated(ticket);
}

条件 == true 生成的令牌具有默认的过期时间(15 分钟).我不想更改 context.Options.AccessTokenExpireTimeSpan,因为它会影响所有令牌,这不是我的想法.

The generated token with condition == true has the default expiration time (15 minutes). I would like to not change the context.Options.AccessTokenExpireTimeSpan because it affects all tokens and that's not the idea.

推荐答案

你必须在 TokenEndPoint 方法而不是 GrantResourceOwnerCredentials 方法中设置过期时间:

You have to set the expiration time in the TokenEndPoint method instead of GrantResourceOwnerCredentials method:

public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    ...

    if (condition)
    {
        context.Properties.ExpiresUtc = DateTime.UtcNow.AddDays(14);
    }

    ...
}

希望能帮到你.

编辑

正如 Michael 在他的 响应 对类似问题,如果您对每个 client_id 有不同的 AccessTokenExpireTimeSpan,您可以在客户端的上下文选项中覆盖默认配置的 AccessTokenExpireTimeSpan验证客户端身份验证时的一种:

As pointed by Michael in his response to a similar question, if you have a different AccessTokenExpireTimeSpan for each client_id you can override the default configured AccessTokenExpireTimeSpan in the context options with the client one when validating the client authentication:

public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
    ...

    context.Options.AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(client.AccessTokenExpireTime);

    ...
}

这篇关于覆盖 AccessTokenExpireTimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆