Reputation: 220
Trying to get the current logon username on client side in asp web app.
I am able to get the username like this
<domain_name>/<user>
CCI\Waqar
But I want to extract the client name in this format
Waqar.Ahmed/Technology/Lahore
I can get it while running on Localhost. But when I deploy the package on web server it gives me exception.
Following is the code by which i am able to get on Localhost.
try
{
UserPrincipal userPrincipal = UserPrincipal.Current;
sUser = userPrincipal.DisplayName;
}
catch (Exception ex)
{
return ex.Message.ToString();
}
tried googling a lot but no luck.
Upvotes: 0
Views: 411
Reputation: 6814
Two things
domain\login
and cannot be xxx/xxx/xxx as per your example. Most likely you refer to a display name, and/or department/location or other attributes of the user. See How to get "Company" and "Department" from Active Directory given a UserPrincipal object? for more details.the difference between test and server environment is due to different accounts used on IIS (or different web.config). Check IIS settings and also try to use the domain context e.g.
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.Current;
Upvotes: 1