Sunday, October 14, 2012

How to get the domain name and username?

This article shows How to get the Windows current logged user name using ASP.NET. Namespaces :C#
using System.Security.Principal;
Find_Domain_User()Function
The Find_Domain_User() function is use to get domain username & domain name. Refer the code below C#
public static string Find_Domain_User()
{
string the_user = WindowsIdentity.GetCurrent().Name;
if (the_user != null || the_user != “”)
{
string username = “”, domain_name = “”;
int boundary = the_user.IndexOf(“\\”);
domain_name = the_user.Substring(0, boundary);
username = the_user.Substring(boundary + 1, the_user.Length – boundary – 1);
return username;
}
return null;
}
Hope this will help you !!!

No comments:

Post a Comment