Tag Archives: .net

Get Logged In User With ASP.Net

Using the following three ways we can get the User Name using C#

1
2
System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
string strName = p.Identity.Name;

1
string strName = HttpContext.Current.User.Identity.Name.ToString();

1
2
string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name
string strName = Request.ServerVariables[5]; //Finding with index

In Above 3 Cases returning string contains DomainName\WinNTLoggedUserName

(for Ex: Microsoft\Bill.Gates. Here Microsoft is domain Bill.Gates is Logger User Name )

Using string operations seperate the DomainName and UserName.

C# .Net – Save Text Box Content To TXT File

A simple way to store the contents of a text box control to a local .txt file

1
2
3
4
5
6
7
8
//Use StreamWriter class.
StreamWriter sw = new StreamWriter("E:\\test.txt");
 
//Use write method to write the text
sw.Write(textBox1.Text);
 
//always close your stream
sw.Close();