使用访问令牌asp.net身份检查每个请求的SecurityStamp

时间:2019-02-23 05:14:50

标签: c# asp.net asp.net-identity

我正在使用asp.net身份保护我的api, 我使用以下功能为用户登录时创建访问令牌

private string GenerateAccessToken(string userName, string role)
        {
            ClaimsIdentity oAuthIdentity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType);

            oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, userName));
            oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, role));

            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties());

            DateTime currentUtc = DateTime.UtcNow;
            ticket.Properties.IssuedUtc = currentUtc;
            ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(365));

            string accessToken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);

            Request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

            return accessToken;
        }

一切正常,直到执行帐户密码更新,然后再更新SecurityStamp

UserManager.UpdateSecurityStampAsync(loggedinUser.Id);

,但是问题在于令牌仍然可以毫无问题地用于调用我的api。那么我该如何检查每个请求的SecurityStamp?

0 个答案:

没有答案