I created a Server side Blazor application using Windows authentication. And it created the following file.
LoginDisplay.razor
<AuthorizeView>
Hello, @context.User.Identity.Name!
</AuthorizeView>
However, it shows "DOMAIN\username". Is it a way to show the user first name?
I tried to print all the claim types.
@foreach(var c in context.User.Claims)
{
<p>@c.Value @c.Type</p>
}
However, there is only one type with name in it. (with value of DOMAIN\username
)
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
You have to go out to AD for that information. But connecting to AD in ASP.NET Core isn't straight-forward.
If you are only going to run this on a Windows server, then you can install Microsoft.Windows.Compatibility
from NuGet, and use DirectoryEntry
to bind directly to the object using its SID. The SID is available in context.User.Identity.User.Value
.
<AuthorizeView>
@{
var identity = (WindowsIdentity) context.User.Identity;
var user = new DirectoryEntry($"LDAP://<SID={identity.User.Value}>");
//Add any other attributes you want to read to this list
user.RefreshCache(new [] { "givenName" });
}
Hello, @user.Properties["givenName"].Value!
</AuthorizeView>
Other attributes you might be interested in:
sn
: Last namedisplayName
: How their name is displayed in Outlook, for example. Often this is "Last, First"