How do I get the full name of a user in .net MVC 3 intranet app?

前端 未结 4 759
南方客
南方客 2020-12-23 14:16

I have an MVC 3 intranet application that performs windows authentication against a particular domain. I would like to render the current user\'s name.

in the view,<

4条回答
  •  隐瞒了意图╮
    2020-12-23 14:53

    Another option, without requiring a helper... You could just declare context and principal before you need to utilize these values, and then utilize it like a standard output...

    @{ // anywhere before needed in cshtml file or view
        var context = new PrincipalContext(ContextType.Domain);
        var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
    }
    

    Then anywhere within the document, just call each variable as needed:

    @principal.GivenName // first name
    @principal.Surname // last name
    

提交回复
热议问题