我有一个Windows窗体应用程序,用户可以登录。该应用程序是独立的,不与任何人或任何人连接。获取用户登录的价值的最佳方式是什么?
除了创建一个全局变量,我怎么能有一个容易访问的变量来检查当前用户的权限?
一个不太明智的做事方式就是在Form构造函数中传递userType的ID,并根据它,.Enable = false;他们没有权限使用的按钮。
谢谢!
我有一个Windows窗体应用程序,用户可以登录。该应用程序是独立的,不与任何人或任何人连接。获取用户登录的价值的最佳方式是什么?
除了创建一个全局变量,我怎么能有一个容易访问的变量来检查当前用户的权限?
一个不太明智的做事方式就是在Form构造函数中传递userType的ID,并根据它,.Enable = false;他们没有权限使用的按钮。
谢谢!
如果你想的当前登录的Windows用户的ID(即应用程序运行时所使用的用户),也有得到它的方法有两种:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
在启动,您可以使用Thread.CurrentPrincipal
来获取用户的安全主体。WindowsIdentity.GetCurrent()
来获取当前用户的身份。然后您可以使用new WindowsPrincipal(identity)
创建安全主体。这些两者是等价的,并且将让你security principal有一个IsInRole方法可用于检查权限。
使用WindowsIdentity类获取System.Security.Principal.WindowsIdentity下的用户标识。
WindowsIdentity current = WindowsIdentity.GetCurrent();
Console.WriteLine("Name:" + current.Name);
使用WindowsPrincipal类获取用户角色,在System.Security.Principal.WindowsPrincipal下找到。
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(current);
if (principal.IsInRole("your_role_here")
{
Console.WriteLine("Is a member of your role");
}