我想知道最好的方法是什么,用我自己的自定义 GenericPrincipal 替换 genericPrincipal。
目前我有类似的东西,但我不确定它是否正确。
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
var identity = new CustomIdentity(authTicket);
var principal = new CustomPrincipal(identity);
Context.User = principal;
}
else
{
//Todo: check if this is correct
var genericIdentity = new CustomGenericIdentity();
Context.User = new CustomPrincipal(genericIdentity);
}
}
我需要替换它,因为我需要一个实现 ICustomPrincipal 接口的委托人,因为我正在使用 Ninject 执行以下操作:
Bind<ICustomPrincipal>().ToMethod(x => (ICustomPrincipal)HttpContext.Current.User)
.InRequestScope();
那么替换 GenericPrincipal 的最佳方法是什么?
提前致谢,
Pickels
你忽略了一个微妙的细节,那就是线索。
Context.User = Thread.CurrentPrincipal = new CustomPrincipal....
会带你去你需要去的地方。
我还注意到您提到您只需要更换校长。如果是这种情况,您可以简单地重用已经为您构建的 FormsIdentity,如下所示。
Context.User = Thread.CurrentPrincipal = new CustomPrincipal(Context.User.Identity /*, add roles here if desired*/ );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)