首页 > 解决方案 > C# OWIN Security - 设置过期令牌 - 始终具有默认值

问题描述

大家好,我目前有一个使用 owin security 的项目

当我尝试向 / 令牌发出请求时,我得到了这个

在此处输入图像描述

那里指定到期令牌为 7199 秒(2 小时)

我正在寻找这个端点(路线),但我不喜欢 /token它或找到他们将此值设置为 2 小时的地方(查看整个解决方案)

我发现的唯一一件事是这个类对应于刷新令牌(但没有过期令牌),但是这个令牌设置为 14400,但是当我再次发出请求时,令牌总是保持在那个值

namespace Conarch.Providers
{
    public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
    {

        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var clientid = context.Ticket.Properties.Dictionary["as:client_id"];

            context.Ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(12000));

            if (string.IsNullOrEmpty(clientid))
            {
                return;
            }

            var refreshTokenId = Guid.NewGuid().ToString("n");

            using (AuthRepository _repo = new AuthRepository())
            {
                var refreshTokenLifeTime = context.OwinContext.Get<string>("as:clientRefreshTokenLifeTime"); 

                var token = new RefreshToken() 
                { 
                    Id = Helper.GetHash(refreshTokenId),
                    ClientId = clientid, 
                    Subject = context.Ticket.Identity.Name,
                    IssuedUtc = DateTime.UtcNow,
                    ExpiresUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime)) 
                };

                context.Ticket.Properties.IssuedUtc = token.IssuedUtc;
                context.Ticket.Properties.ExpiresUtc = token.ExpiresUtc;

                token.ProtectedTicket = context.SerializeTicket();

                var result = await _repo.AddRefreshToken(token);

                if (result)
                {
                    context.SetToken(refreshTokenId);
                }

            }
        }

        public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {

            var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

            string hashedTokenId = Helper.GetHash(context.Token);

            using (AuthRepository _repo = new AuthRepository())
            {
                var refreshToken = await _repo.FindRefreshToken(hashedTokenId);

                if (refreshToken != null )
                {
                    //Get protectedTicket from refreshToken class
                    context.DeserializeTicket(refreshToken.ProtectedTicket);
                    var result = await _repo.RemoveRefreshToken(hashedTokenId);
                }
            }
        }

        public void Create(AuthenticationTokenCreateContext context)
        {
            throw new NotImplementedException();
        }

        public void Receive(AuthenticationTokenReceiveContext context)
        {
            throw new NotImplementedException();
        }

我的问题是:你在什么地方设置这个值,时间怎么会增加?

非常感谢你

标签: c#asp.netoauthasp.net-identityowin

解决方案


您必须在 Web 应用程序配置期间设置过期时间使用此:

OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()  
{  

    AllowInsecureHttp = true,  
    TokenEndpointPath = new PathString("/token"),  
    AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),  
    Provider = new AuthorizationServerProvider(),  
    RefreshTokenProvider = new RefreshTokenProvider()  
};

你可以在这里找到完整的文章


推荐阅读