Asp.net web api 从数据存储实现动态OAuthBearerServerOptions AccessTokenExpireTimeSpan值

Asp.net web api 从数据存储实现动态OAuthBearerServerOptions AccessTokenExpireTimeSpan值,asp.net-web-api,oauth,token,owin,Asp.net Web Api,Oauth,Token,Owin,本文的上下文涉及ASP.NETWebAPI2.2+OWIN 该环境是一个具有OWIN服务器和Web Api的单一应用程序 背景: 在Startup类中,必须指定提供给的。这些选项是在OWIN服务器启动期间创建的。在上,我必须指定,以便确保令牌过期 问题 我必须能够根据每个身份验证请求动态指定过期时间跨度。我不确定是否可以这样做,我想知道: 能做到吗 若有,;我可以在哪一点执行此查找和过期分配 启动配置的内容: var config = new HttpConfiguration();

本文的上下文涉及ASP.NETWebAPI2.2+OWIN 该环境是一个具有OWIN服务器和Web Api的单一应用程序

背景

在Startup类中,必须指定提供给的。这些选项是在OWIN服务器启动期间创建的。在上,我必须指定,以便确保令牌过期

问题

我必须能够根据每个身份验证请求动态指定过期时间跨度。我不确定是否可以这样做,我想知道:

  • 能做到吗
  • 若有,;我可以在哪一点执行此查找和过期分配
启动配置的内容:

    var config = new HttpConfiguration();

    WebApiConfig.Register(config);


    var container = builder.Build();
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    var OAuthServerOptions = new OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/OAuth"),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(**THIS NEEDS TO BE DYNAMIC**)),
        Provider = new AuthorizationServerProvider()
    };

    //STOP!!!!!!!!
    //DO NOT CHANGE THE ORDER OF THE BELOW app.Use statements!!!!!

    //Token Generation 
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); //this MUST come before oauth registration
    app.UseOAuthAuthorizationServer(OAuthServerOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
    {
        Provider = new BearerProvider()
    });
    app.UseAutofacMiddleware(container); //this MUST come before UseAutofacWebApi
    app.UseAutofacWebApi(config);//this MUST come before app.UseWebApi
    app.UseWebApi(config);
我开始处理BearProvider类(请参见上面的app.useAuthBeareAuthentication了解我在哪里使用这个类)以及具体的ValidateIdentity方法,但不确定这是否是身份验证工作流中设置此值的正确点。这似乎是恰当的,但我寻求证实我的立场

public class BearerProvider : OAuthBearerAuthenticationProvider
{
    public override async Task RequestToken(OAuthRequestTokenContext context)
    {
        await base.RequestToken(context);

        //No token? attempt to retrieve from query string
        if (String.IsNullOrEmpty(context.Token))
        {
            context.Token = context.Request.Query.Get("access_token");

        }
    }
    public override Task ValidateIdentity(OAuthValidateIdentityContext context)
    {
        //context.Ticket.Properties.ExpiresUtc= //SOME DB CALL TO FIND OUT EXPIRE VALUE..IS THIS PROPER?
        return base.ValidateIdentity(context);
    }
}

提前谢谢

所以我完全站错了位置。最后我不得不使用我的自定义OAuthorizationServerProvider,在该自定义类中重写的GrantResourceOwnerCredentials方法中,我能够通过访问。。。

财产

 <!-- language: c# -->

    public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
        {
            public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
               //DO STUFF
               var expireValue=GetTimeOutFromSomeplace();
               context.Options.AccessTokenExpireTimeSpan = expireValue;
               //DO OTHER TOKEN STUFF
            } 
        }

公共类AuthorizationServerProvider:OAuthAuthorizationServerProvider
{
公共重写异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentials上下文)
{
//做事
var expireValue=GetTimeOutFromSomeplace();
context.Options.AccessTokenExpireTimeSpan=expireValue;
//做其他象征性的事情
} 
}

设置context.Options.AccessTokenExpireTimeSpan实际上会更改全局值,并影响所有不适用于原始需求的请求

正确的位置是TokenEndpoint方法

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

    if (someCondition)
    {
        context.Properties.ExpiresUtc = GetExpirationDateFromDB();
    }

    ...
}

谢谢你提供的信息。我现在已经不在这个项目上了,但如果我要验证,我会这样做,并用确认和可能的答案更改来更新帖子。我知道这个答案现在有点老了,但这对我不起作用。无论我在
ExpiresUtc
@Tom中设置了什么,返回令牌中的“.expires”时间仍然是默认的20分钟,您确定吗?我只是重新测试,它仍然对我有效。您使用的是Microsoft.Owin的哪个版本,您可以分享一些您如何使用它的代码吗?这不是正确的答案。如果正在处理多个客户端请求,则您正在创建竞争条件。这是否是因为更新AccessTokenExpireTimeSpan?正是如此。假设客户A进来,你希望它设置为1小时。GrantResourceOwnerCredentials开始处理它,您可以将context.Options.AccessTokenExpireTimeSpan更改为1小时。现在客户B进来了,您希望它设置为1年。它将context.Options.AccessTokenExpireTimeSpan更改为1年。现在,客户端A和B都最终退出GrantResourceOwnerCredentials,并且都将在1年内到期。这非常有意义。非常感谢。上面狄晨的回答是否更准确地回答了这个问题?它“看起来”像是我想要的,但我已经好几个月没有参与那个项目了(停滞了),所以我还没有测试出来。我同意。文件记录糟糕透顶。
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    ...

    if (someCondition)
    {
        context.Properties.ExpiresUtc = GetExpirationDateFromDB();
    }

    ...
}