【发布时间】:2013-10-10 20:10:02
【问题描述】:
作为一种身份验证机制,azure stodge 有机会提供有限期限的令牌。记录在用户的会话中,并在与用户一起到网站检查其有效性的过程中。最后只是提供用户再次登录。 可以建议这个方案是如何正确的? 使用 azure stodge 登录是否有类似的解决方案?
【问题讨论】:
标签: asp.net-mvc azure authorization storage
作为一种身份验证机制,azure stodge 有机会提供有限期限的令牌。记录在用户的会话中,并在与用户一起到网站检查其有效性的过程中。最后只是提供用户再次登录。 可以建议这个方案是如何正确的? 使用 azure stodge 登录是否有类似的解决方案?
【问题讨论】:
标签: asp.net-mvc azure authorization storage
对于令牌身份验证,您最好使用 OAuth 或 OAuth2。它完全符合您的要求,但您可以创建一个操作过滤器来实现相同的功能。
这是一个带有 WebApi 控制器的示例,很容易切换到 mvc 控制器。基本上,您不会在 http 标头中拥有令牌,而是在查询字符串中。
[AuthKey]
public class PurchaseController : ApiController
{
//methods
}
public class AuthKeyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Contains("auth-key") == false)
actionContext.Response = new System.Net.Http.HttpResponseMessage(HttpStatusCode.Unauthorized);
else
{
var token = actionContext.Request.Headers.GetValues("auth-key").First();
var sessionServices = new SessionServices();
var session = sessionServices.SearchSessionByAuthKey(token);
if (session == null || session.expirationDate < DateTime.Now)
{
actionContext.Response = new System.Net.Http.HttpResponseMessage(HttpStatusCode.Unauthorized);
return;
}
var controller = actionContext.ControllerContext.Controller;
if (controller is BaseController)
{
var baseController = (BaseController)controller;
baseController.SetThreadLocalSession(session);
}
base.OnActionExecuting(actionContext);
}
}
}
【讨论】: