我知道这个话题已经涨了不少,但我还没有找到一个我的问题工程..HttpContext.Current.Session总是空
我有一个GuestTokenValidationAttribute类,从ActionFilterAttribute派生,在那里我从头部收到一个令牌,并将其用作String令牌。然后我想将该令牌添加到会话中,但无论我做什么,Session都始终为空。
请你们任何指导或帮助将非常感激,
代码下面的示例:
public class GuestTokenValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
string token;
try
{
token = actionContext.Request.Headers.GetValues("Authorization-Token").First();
}
catch (Exception)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Unauthorized User")
};
return;
}
if(string.IsNullOrEmpty(token))
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Unauthorized User")
};
return;
}
try
{
var repository = DependencyResolver.Current.GetService<IRepository<Guest>>();
var guest = repository.GetAll().FirstOrDefault(x => x.Token == token);
if(guest == null)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Unauthorized User")
};
return;
}
}
catch (Exception)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Unauthorized User")
};
return;
}
HttpContext.Current.Session.Add("guesttoken" ,token);
base.OnActionExecuting(actionContext);
}
您是否在web.config中启用了会话? – RononDex
我是否有 –
这是API控制器吗? –