首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >强制身份验证令牌过期

强制身份验证令牌过期
EN

Stack Overflow用户
提问于 2015-02-26 16:40:44
回答 2查看 5.7K关注 0票数 0

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

如何管理强制使我的访问令牌过期?或者,有没有其他方法来处理这种情况?

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

更新:

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

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

EN

回答 2

Stack Overflow用户

发布于 2015-02-26 16:47:16

如果sign out不执行此操作,则需要删除cookie和会话。

代码语言:javascript
运行
复制
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

票数 0
EN

Stack Overflow用户

发布于 2015-02-26 17:04:45

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

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

  1. 在注销时清除所有会话和cookies。
  2. 您还可以自定义自动筛选并生成自定义访问令牌,并将其存储到具有超时限制的本地文件或数据库中。注销时,您可以根据用户清除令牌。

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

代码语言:javascript
运行
复制
 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上使用此自定义filer,如下所示

代码语言:javascript
运行
复制
[CustomAuthenticate]
public ActionResult Index()
{
return View();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28737778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档