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

时间:2017-06-28 17:50:28

标签: asp.net-mvc asp.net-web-api model-view-controller oauth

我在MVC项目中使用MVC Web API。我使用AuthorizeForAPI自定义属性来验证令牌。一切都很好。我的问题是如何验证令牌到期日期,所以如果令牌已过期,我将从服务器发送消息告诉用户您的令牌已过期

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");

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用

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

     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"
        }