强制认证令牌过期

时间:2015-02-26 08:40:44

标签: c# asp.net-mvc asp.net-web-api asp.net-web-api2

我花了一周的时间来保护我的Web API,创建自定义过滤器和使用身份验证令牌。我现在的问题是当我使用POSTMAN请求我的Web API并且用户已经注销我仍然可以从我的API获取值。

如何设法强制使我的访问令牌过期?还是有其他办法来管理这种情况吗?

注意:当我请求使用POSTMAN时,我从本地存储中复制了访问令牌。

更新

这是我在创建访问令牌时所遵循的。 http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

我尝试了与我下载的解决方案相同的情况,我的访问令牌仍然经过身份验证

2 个答案:

答案 0 :(得分:0)

如果退出没有这样做,您需要删除Cookie和会话。

FormsAuthentication.SignOut();
Session.Abandon();

// clear authentication cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);

// clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);

FormsAuthentication.RedirectToLoginPage();

Refrence Taken from here

答案 1 :(得分:0)

根据http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

在本文中,有关授权令牌的所有详细信息都会与Cookie一起存储在会话中。所以你有两种方法来解决这个问题。

  1. 在注销时清除所有会话和Cookie。

  2. 您还可以创建自定义自动筛选过滤器并生成自定义访问令牌,并将其存储到具有超时限制的本地文件或数据库。注销时,您可以根据用户清除令牌。

  3. 这是一个例子,如何在web api 2中设置自定义过滤器。

     public class CustomAuthenticateAttribute : Attribute, IAuthenticationFilter
        {
    
            public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
            {
                HttpRequestMessage request = context.Request;
                AuthenticationHeaderValue authorization = request.Headers.Authorization;
    
                if (authorization == null)
                    return;
    
                if (authorization.Scheme != "Bearer")
                    return;
    
                if (String.IsNullOrEmpty(authorization.Parameter))
                {
                    context.ErrorResult = new AuthenticationFailureResult("Missing token", request);
                    return;
                }
    
                TokenL1 tokenL1;
                var validateToken = TokenHelper.DecryptToken(authorization.Parameter, out tokenL1);
                if (!validateToken)
                {
                    context.ErrorResult = new AuthenticationFailureResult("Token invalid", request);
                    return;
                }
                if (!(tokenL1.tokenexpiry > DateTime.Now))
                {
                    context.ErrorResult = new AuthenticationFailureResult("Token expire", request);
                    return;
                }
                IPrincipal principal = new GenericPrincipal(new GenericIdentity(tokenL1.email), new string[] { "user" });
    
                if (principal == null)
                {
                    context.ErrorResult = new AuthenticationFailureResult("Invalid token", request);
                    return;
                }
                else
                {
                    context.Principal = principal;
                }
            }
    
            public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
            {
                var challenge = new AuthenticationHeaderValue("Bearer");
                context.Result = new AddChallengeOnUnauthorizedResult(challenge, context.Result);
                return Task.FromResult(0);
            }
            public bool AllowMultiple
            {
                get { return false; }
            }
        }
    

    在像这样的控制器的actionresult上使用这个自定义文件管理器

    [CustomAuthenticate]
    public ActionResult Index()
    {
    return View();
    }
    
相关问题