如何捕获我的下面代码中包含的WINDOWS USERNAME?

Yve*_*ves 5 c# asp.net

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
Run Code Online (Sandbox Code Playgroud)

ser*_*hio 5

请参阅Environment.UserName

Console.WriteLine("UserName: {0}", System.Environment.UserName);
Run Code Online (Sandbox Code Playgroud)

  • 抱歉-1,但这个答案在ASP.NET上下文中是不正确的.不希望任何来自搜索引擎的人看到这个接受的答案,并忽略下面的正确答案. (2认同)

Col*_*ard 5

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

如果您使用Environment.UserNameASP.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;
    }
}
Run Code Online (Sandbox Code Playgroud)