【问题标题】:Asp.net: Replace GenericPrincipalAsp.net:替换 GenericPrincipal
【发布时间】:2011-02-15 14:14:09
【问题描述】:

我想知道用我自己的 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 的最佳方法是什么?

提前致谢,

泡菜

【问题讨论】:

    标签: asp.net asp.net-mvc security asp.net-membership


    【解决方案1】:

    你错过了一个微妙的细节,线程。

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

    会带你去你想去的地方。

    我还注意到你提到你只需要更换校长。如果是这种情况,您可以简单地重用已经为您构建的 FormsIdentity,如下所示。

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

    【讨论】:

    • 感谢您的回答和额外的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2015-01-11
    • 1970-01-01
    • 2013-03-08
    • 2016-08-10
    相关资源
    最近更新 更多