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
Console.WriteLine("UserName: {0}", System.Environment.UserName);
От: http://jerrytech.blogspot.com/2008/04/current-user-in-aspnet-it-not.html
Если вы используете Environment.UserName
для получения текущего пользователя в приложении ASP.Net, вы, вероятно, получите «Сетевую службу», потому что это пользователь, работающий с IIS (если другой пользователь не использует IIS) .
Если вы хотите получить текущего пользователя, просто сделайте следующее:
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;
}
}
приложение Windows Form: Формы. SystemInformation. UserName
MessageBox. Показать (Система. Windows. Формы. SystemInformation. UserName);
Для веб-сайта Asp.net: Пользователь. Идентичность. Имя
HttpContext. Ток. Пользователь. Идентичность. Имя
Другие пути
public string GetUserName()
{
return System.Environment.UserName;
//Gets the name of the user who started this thread
}
Используя WMI (Инструментовка управления Windows):
Система использования. Управление;
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;
}
Unamanaged путь: Ссылка GetUserName API
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);
Используя WindowsIdentity
using System.Security.Principal;
public string GetUserName()
{
return( WindowsIdentity.GetCurrent().Name);
}