Friday 13 April 2012

Get the current username from windows authentication or form base authentication

string userName = " ";
SPContext currentContext;
try
{
currentContext = SPContext.Current;
}
catch (InvalidOperationException)
{
currentContext = null;
}
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
//userName = SPContext.Current.Web.CurrentUser.LoginName;
userName = HttpContext.Current.User.Identity.Name;
}
else
{
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
if (windowsIdentity != null)
{
userName = windowsIdentity.Name;
}
}
return userName;

No comments:

Post a Comment