我已经花了一周的时间来保护我的Web API,创建自定义过滤器并使用身份验证令牌。我现在的问题是,当我使用POSTMAN在我的Web API中请求,而用户已经注销时,我仍然可以从我的API中获得值。
如何管理强制使我的访问令牌过期?或者,有没有其他方法来处理这种情况?
注意:当我使用POSTMAN请求时,我从本地存储复制了我的访问令牌。
更新:
这就是我在创建访问令牌时所遵循的。http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
我对下载的解决方案尝试了与我相同的情况,但我的访问令牌仍然通过了身份验证
发布于 2015-02-26 16:47:16
如果sign out不执行此操作,则需要删除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();发布于 2015-02-26 17:04:45
根据http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
在本文中,关于授权令牌的所有细节都与cookie一起存储在会话中。所以你有两种方法来解决这个问题。
这里有一个例子,如何在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上使用此自定义filer,如下所示
[CustomAuthenticate]
public ActionResult Index()
{
return View();
}https://stackoverflow.com/questions/28737778
复制相似问题