Asp.net:替换 GenericPrincipal
我想知道最好的方法是将 genericPrincipal 替换为我自己的 CustomGenericPrincipal。
目前我有类似的东西,但我不确定它是否正确。
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 的最佳方法是什么?
预先感谢,
皮克斯
I was wondering what the best way is to replace the genericPrincipal with my own CustomGenericPrincipal.
At the moment I have something like this but I aint sure if it's correct.
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);
}
}
I need to replace it because I need a Principal that implements my ICustomPrincipal interface because I am doing the following with Ninject:
Bind<ICustomPrincipal>().ToMethod(x => (ICustomPrincipal)HttpContext.Current.User)
.InRequestScope();
So what's the best way to replace the GenericPrincipal?
Thanks in advance,
Pickels
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你忽略了一个微妙的细节,那就是线索。
会带你去你需要去的地方。
我还注意到您提到您只需要更换校长。如果是这种情况,您可以简单地重用已经为您构建的 FormsIdentity,如下所示。
You are missing a subtle detail, the thread.
Will get you where you need to go.
Also I notice you mention that you need to only replace the principal. If this is the case, you can simply reuse the FormsIdentity that has already been constructed for you as shown below.