MVC 2.0中用户对象的自定义标识

时间:2011-01-14 18:59:16

标签: c# .net asp.net-mvc-2

我正在尝试获取Identity对象的自定义字段,以便在页面方面,我可以简化对登录用户的引用。

    WizBang.aspx

    Welcome <b><%: Page.User.Identity.FirstName %></b>
    Your username is<%: Page.User.Identity.Name %>!

我的另一个想法是将它粘贴到会话中并通过Application_PostAuthenticateRequest监听器进行设置,而不是首先弄乱整个身份。或者,如果有更好的其他方式。

1 个答案:

答案 0 :(得分:0)

Application_PostAuthenticateRequest是设置自定义IIdentity的正确方法。我建议你为你的身份添加一个静态属性:

public MyCustomIdentity : IIdentity
{
    public static MyCustomIdentity Current { get { return (MyCustomIdentity) Thread.CurrentPrincipal.Identity; }}
}

并在您的视图中访问<%= MyCustomIdentity.Current.SpecialProperty %>