目前,我在 OnValidateIdentity 中访问 Session 时遇到问题 -HttpContext.Current.Session
一片空白。怎么了?我的申请如下:
我希望用户在更改密码时注销 - >更改安全标记。
我实现为:Mvc 项目将验证 SecurityStamp 在用户请求时更改。我将从其他 webapi 网站获取 SecurityStamp 。这意味着我的 mvc 无法通过 webapi 直接访问数据库。我必须在授权标头中输入令牌才能从 webapi 获取 securitystamp。但是,我无法从 session 访问令牌,当我成功登录时,我将令牌存储在 Session 中。代码示例:
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 管道中的身份验证阶段运行,该阶段先于HttpContext
或会话状态可用。所以你需要在没有它的情况下工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)