【发布时间】:2014-07-20 07:39:28
【问题描述】:
在登录算法中的 ASP.NET Web API 中,我有一个动作过滤器,它为每个用户生成一个令牌,前端通过在 Web 服务器中使用该令牌将该令牌发送回以对用户进行身份验证我可以获得当前用户到目前为止的信息每件事都运行良好,但是我有新的要求,即每个用户都与帐户有多对多的关系,这意味着同一用户可以存在于多个具有不同角色的帐户中,例如在帐户 1 中,他是帐户 2 中的管理员他是普通用户,所以我必须重新生成需要用户重新登录的令牌,我不希望他再次被重定向到登录页面。我的想法是将用户名和密码存储在 html 5 本地存储中,但我认为这是一种不好的做法。 她是我生成令牌的方式。
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.Request.Headers
.Any(header => header.Key == "AuthorizationHeader"))
{
if (this.IsAnonymousAllowed(actionContext) == false)
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "Un Autherized");
}
}
else
{
string token = actionContext.Request.Headers
.Where(header => header.Key == "AuthorizationHeader")
.First().Value.First();
if (this.IsAnonymousAllowed(actionContext) == true)
{
return;
}
string passPhrase = System.Configuration.ConfigurationSettings.AppSettings["PassPhrase"];
string ticket_string = Crypto.Decrypt(token, passPhrase);
TicketData ticket = JsonConvert.DeserializeObject<TicketData>(ticket_string);
if (ticket == null || ticket.Expiration < DateTime.Now)
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, "UnAuthorized");
}
else
{
OurIdentity identity = (OurIdentity)ticket.TokenData.OurIdentity;
System.Threading.Thread.CurrentPrincipal = new OurPrincipal
{
OurIdentity = identity,
};
}
}
}
【问题讨论】:
标签: asp.net authentication asp.net-web-api