Showing posts with label form base authentication. Show all posts
Showing posts with label form base authentication. Show all posts

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;