How can I grab the WINDOWS USERNAME included in my code below?

private void UndeletableComments(LinqDataSourceUpdateEventArgs e) { //get a reference to the currently saved item ****NOTE (State) is the ClassName. It's a table of states in this test database var currentData = ((MyData)e.OriginalObject).Notes; // make a copy of whatever is in the edit field and strip out the previous comments var newData = ((MyData)e.NewObject).Notes.Replace(currentData, string.Empty); //check both values for nulls if (currentData != null && newData != null) { newData = ((MyData)e.NewObject).Notes.Replace(currentData, string.Empty); } // replace the data to be stored in the database with the currentdata + the newData // I added a datestamp to see when the new comment was added. ((MyData)e.NewObject).Notes = string.Format("{0} Added:{1} at (2) --- {3}", currentData, DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString(), newData); // I need to see the WINDOW USERNAME by capturing who added a new comments 
+4
source share
3 answers

see Environment.UserName

 Console.WriteLine("UserName: {0}", System.Environment.UserName); 
+4
source

From: http://jerrytech.blogspot.com/2008/04/current-user-in-aspnet-it-not.html

If you use Environment.UserName to get the current user in an ASP.Net application, you will probably get a "Network Service" because it is the user running IIS (unless the other user has IIS running).

If you want to get the current user, just do the following:

 public static string CurrentUserName { get { System.Security.Principal.IPrincipal _User; _User = System.Web.HttpContext.Current.User; System.Security.Principal.IIdentity _Identity; _Identity = _User.Identity; string _Value; _Value = _Identity.Name.Substring(_Identity.Name.IndexOf(@"\")+1); return _Value; } } public static string CurrentDomain { get { System.Security.Principal.IPrincipal _User; _User = System.Web.HttpContext.Current.User; System.Security.Principal.IIdentity _Identity; _Identity = _User.Identity; string _Value; _Value = _Identity.Name.Substring(0, _Identity.Name.IndexOf(@"\")); return _Value; } } 
+5
source

other methods

 public string GetUserName() { return System.Environment.UserName; //Gets the name of the user who started this thread } 

Using WMI (Windows Management Instrumentation):

using System.Management;

 public string GetUserName() { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT UserName FROM Win32_ComputerSystem"); string user = string.Empty; foreach (ManagementObject queryObj in searcher.Get()) { user = Convert.ToString(queryObj["UserName"]); } return user; } 

Unmanaged Method: GetUserName API Link

 using System.Runtime.InteropServices; public string GetUserName() { byte[] user = new byte[256]; Int32[] len = new Int32[1]; len[0] = 256; GetUserName(user, len); return (System.Text.Encoding.ASCII.GetString(user)); } [DllImport("Advapi32.dll", EntryPoint = "GetUserName", ExactSpelling = false, SetLastError = true)] static extern bool GetUserName([MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] nSize); 

Using WindowsIdentity

 using System.Security.Principal; public string GetUserName() { return( WindowsIdentity.GetCurrent().Name); } 
+3
source

Source: https://habr.com/ru/post/1300194/


All Articles