Skip to main content
fix code formatting
Source Link
Oskar Berggren
  • 5.7k
  • 1
  • 21
  • 37

For a Windows Forms app that was to be distributed to several users, many of which log in over vpn, I had tried several ways which all worked for my local machine testing but not for others. I came across a Microsoft article that I adapted and works.

using System;

using System.Security.Principal;

namespace ManageExclusion

{

using System;
using System.Security.Principal;

namespace ManageExclusion
{
    public static class UserIdentity

    {
        // concept borrowed from 
        // https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity(v=vs.110).aspx

        public static string GetUser()
        {
            IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
            WindowsIdentity windowsIdentity = new WindowsIdentity(accountToken);
            return windowsIdentity.Name;
        }
    }
}

}

For a Windows Forms app that was to be distributed to several users, many of which log in over vpn, I had tried several ways which all worked for my local machine testing but not for others. I came across a Microsoft article that I adapted and works.

using System;

using System.Security.Principal;

namespace ManageExclusion

{

public static class UserIdentity

{
    // concept borrowed from 
    // https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity(v=vs.110).aspx

    public static string GetUser()
    {
        IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
        WindowsIdentity windowsIdentity = new WindowsIdentity(accountToken);
        return windowsIdentity.Name;
    }
}

}

For a Windows Forms app that was to be distributed to several users, many of which log in over vpn, I had tried several ways which all worked for my local machine testing but not for others. I came across a Microsoft article that I adapted and works.

using System;
using System.Security.Principal;

namespace ManageExclusion
{
    public static class UserIdentity

    {
        // concept borrowed from 
        // https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity(v=vs.110).aspx

        public static string GetUser()
        {
            IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
            WindowsIdentity windowsIdentity = new WindowsIdentity(accountToken);
            return windowsIdentity.Name;
        }
    }
}
Source Link
john rains
  • 401
  • 6
  • 8

For a Windows Forms app that was to be distributed to several users, many of which log in over vpn, I had tried several ways which all worked for my local machine testing but not for others. I came across a Microsoft article that I adapted and works.

using System;

using System.Security.Principal;

namespace ManageExclusion

{

public static class UserIdentity

{
    // concept borrowed from 
    // https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity(v=vs.110).aspx

    public static string GetUser()
    {
        IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
        WindowsIdentity windowsIdentity = new WindowsIdentity(accountToken);
        return windowsIdentity.Name;
    }
}

}