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

时间:2010-02-03 16:29:56

标签: 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

3 个答案:

答案 0 :(得分:5)

请参阅Environment.UserName

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

答案 1 :(得分:5)

来自: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;
    }
}

答案 2 :(得分:3)

其他方式

public string GetUserName()
        {
            return System.Environment.UserName;

            //Gets the name of the user who started this thread
        }

使用WMI(Windows Management Instrumentation):

使用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;

        }

非管理方式:Link 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);


        }