【问题标题】:Force expiration of authentication token强制认证令牌过期
【发布时间】:2018-03-22 06:32:14
【问题描述】:

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

我怎样才能强制使我的访问令牌过期?或者有没有其他方法来处理这种情况?

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

更新:

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

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

【问题讨论】:

  • 您能解释一下您使用什么逻辑来存储身份验证令牌吗?
  • 当使用 POSTMAN 请求时,我只需添加身份验证标头,我的请求仍然经过身份验证。
  • 丹,已经找到解决方案了吗?答案并没有真正帮助我。
  • @RageCompex 我对此的实现是使用刷新令牌并为访问令牌提供较短的生命周期(刷新令牌可能需要 5 分钟和一天或更长时间),请参阅 bitoftech.net/2014/07/16/… 以了解刷新令牌的实现

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


【解决方案1】:

如果退出没有这样做,您需要删除 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

【讨论】:

  • 我在我的自定义 actionfilterattribute 中清除了缓存和会话,并在我的 JS 中删除了本地会话
  • 使用访问令牌时,很可能它与 cookie 无关。
【解决方案2】:

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

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

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

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

这里是一个例子,如何在 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2013-03-24
    • 2017-08-20
    • 2019-04-29
    相关资源
    最近更新 更多