开发者

How can I capture the WINDOWS USERNAME included to my below code?

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.开发者_JAVA技巧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


see the Environment.UserName

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


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 the "Network Service" because that is the user running IIS (unless another user is running IIS).

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

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 application: Forms.SystemInformation.UserName

    MessageBox.Show(System.Windows.Forms.SystemInformation.UserName);

  • For Asp.net web site: User.Identity.Name

    HttpContext.Current.User.Identity.Name

Other ways

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;

        }

Unamanaged way : 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);

Using WindowsIdentity

using System.Security.Principal;
public string GetUserName()
        {                

            return( WindowsIdentity.GetCurrent().Name);


        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜