Ramachandran M

Ramachandran M

  • NA
  • 713
  • 19.9k

Could not fetch windows identity name in web api 2.0

Jan 7 2021 5:30 AM
 Hi Friends,
 
I used below code to get windows authentication but it returns empty string after deployed empty string. Yesterday we implimented https certificate after that application failed.
 
i tried to add impersonate set true and false in web.config but no luck. 
 
string sCurrentUserName = string.Empty;
if (HttpContext.Current.User != null)
{
sCurrentUserName = HttpContext.Current.User.Identity.Name.ToString();
}
if (sCurrentUserName == string.Empty)
sCurrentUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
if (sCurrentUserName != null && sCurrentUserName.Trim().Length > 0)
{
string[] splittedName = sCurrentUserName.Split('\\');
if (splittedName.Length > 0)
{
string sDomain = splittedName[0].ToString();
return (splittedName.Length > 1) ? splittedName[1].ToString().Trim().ToUpper() : splittedName[0].ToString().Trim().ToUpper();
}
}
return string.Empty;
 

Answers (5)