加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Asp.net:替换GenericPrincipal

发布时间:2020-12-16 03:53:40 所属栏目:asp.Net 来源:网络整理
导读:我想知道用我自己的CustomGenericPrincipal替换genericPrincipal最好的方法是什么. 目前我有这样的事情,但我不确定它是否正确. protected void Application_AuthenticateRequest(Object sender,EventArgs e){ HttpCookie authCookie = Request.Cookies[Forms
我想知道用我自己的CustomGenericPrincipal替换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接口的Principal,因为我正在使用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*/ );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读