开发者

Asp.net: Replace 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 =开发者_Go百科 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


You are missing a subtle detail, the thread.

    Context.User = Thread.CurrentPrincipal = new CustomPrincipal....

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.

    Context.User = Thread.CurrentPrincipal = new CustomPrincipal(Context.User.Identity /*, add roles here if desired*/ );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜