简体   繁体   English

OAuth 2.0如何验证令牌的到期日期

[英]OAuth 2.0 How to validate token expiration date

I am using MVC Web API inside MVC project.I use AuthorizeForAPI custom Attribute to valid the token. 我在MVC项目中使用MVC Web API。我使用AuthorizeForAPI自定义属性来验证令牌。 Everything is working great. 一切都很好。 My question is how to validate token expiration date so if the the token has expired i will send a message from server tells the user that your token has been expired 我的问题是如何验证令牌的到期日期,以便如果令牌已过期,我将从服务器发送一条消息,通知用户您的令牌已过期

public class AuthorizeForAPI : AuthorizeAttribute
{

    public override void OnAuthorization(HttpActionContext actionContext)
    {
        string AccessTokenFromRequest = "";
        if (actionContext.Request.Headers.Authorization != null)
        {
            // get the access token
            AccessTokenFromRequest = actionContext.Request.Headers.Authorization.Parameter;

            var user = HttpContext.Current.User.Identity;
            if (!user.IsAuthenticated)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized user");

            }
        }
    }
}

using 运用

 AccessTokenExpireTimeSpan = TimeSpan.FromDays(22), //22 day b4 expired

also

     var user = HttpContext.Current.User.Identity;
                    if (!user.IsAuthenticated)
                    {
                        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "Unauthorized user");
                    } 
    else
        {    
actionContext.Request.CreateErrorResponse(HttpStatusCode.NonAuthoritativeInformation, "Expired Token"
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM