目前,我在OnValidateIdentity中访问Session时遇到问题 – HttpContext.Current.Session为null.我错了什么我的申请如下: – 我有2个项目:Mvc vs WebApi – 我希望用户在我更改密码时会退出 – 改变安全
– 我有2个项目:Mvc vs WebApi
– 我希望用户在我更改密码时会退出 – >改变安全标记.
– 我实现为:Mvc项目将验证用户请求时更改的SecurityStamp.我将从其他webapi网站获得SecurityStamp.这意味着我的mvc无法通过webapi直接访问数据库.我必须在authorize标头中输入令牌以从webapi获取安全性标记.但是,我无法从会话访问令牌,当我成功登录时,我将令牌存储在会话中.代码示例:
public void ConfigureAuthentication(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
CookieSecure = CookieSecureOption.SameAsRequest,
LoginPath = new PathString("/Home"),
LogoutPath = new PathString("/Account/Logout"),
ExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = async ctx =>
{
var claim = ctx.Identity.FindFirst("SecurityStamp");
var accessToken = HttpContext.Current.Session["token"].ToString();
using (HttpClient httpClient = new HttpClient())
{
// Used accessToken variable for httpClient
// TODO Get security stamp from webapi . Ex :
string securityStampWebApi = "demo";
if (securityStampWebApi != claim.Value)
{
ctx.RejectIdentity();
}
}
}
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
建议其他实施我可以完成这个案子.
cookie中间件在IIS管道中的身份验证阶段运行,该阶段在HttpContextor会话状态可用之前.所以你需要在没有它的情况下工作.