从访问令牌asp.net身份获取时间已过期

时间:2019-03-08 09:33:50

标签: asp.net asp.net-identity

我正在使用以下方式创建访问令牌以保护我的Webapi的安全,因此如何从每个调用api的令牌中获取过期时间

public static string GenerateAccessToken(int id, string userName)
        {
            ClaimsIdentity oAuthIdentity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType);
            oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, userName));
            oAuthIdentity.AddClaim(new Claim("userid", id.ToString()));
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties());
            DateTime currentUtc = DateTime.UtcNow;
            ticket.Properties.IssuedUtc = currentUtc;
            ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(30));
            string accessToken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);
            //Request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

            return accessToken;
        }

0 个答案:

没有答案