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,<
If you have many controllers then using @vcsjones approach might be painfull. Therefore I'd suggest creating extension method for TIdentity.
public static string GetFullName(this IIdentity id)
{
if (id == null) return null;
using (var context = new PrincipalContext(ContextType.Domain))
{
var userPrincipal = UserPrincipal.FindByIdentity(context, id.Name);
return userPrincipal != null ? $"{userPrincipal.GivenName} {userPrincipal.Surname}" : null;
}
}
And then you can use it in your view:
Hello, @User.Identity.GetFullName()!