我有一个Windows窗体应用程序,用户可以登录。该应用程序是单独的,并且不与任何东西或任何人连接。
除了创建一个全局变量,我怎么能有一个容易访问的变量来检查当前用户的权限?
一种不太合理的做法是在表单构造函数中传递userType的ID,根据该ID,.Enable = false;他们没有权限使用按钮。
谢谢!
发布于 2010-06-24 07:24:57
如果您想要当前登录的Windows用户的id (即运行应用程序的用户),有两种方法可以获取它:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);放入启动程序中,您可以使用Thread.CurrentPrincipal获取用户的安全主体。WindowsIdentity.GetCurrent()获取当前用户的身份。然后,可以使用new WindowsPrincipal(identity).创建安全主体
这两个方法都是等效的,并且会得到一个具有IsInRole方法的security principal,该方法可用于检查权限。
发布于 2010-06-24 07:29:27
使用System.Security.Principal.WindowsIdentity下的WindowsIdentity类获取用户身份。
WindowsIdentity current = WindowsIdentity.GetCurrent();
Console.WriteLine("Name:" + current.Name);使用System.Security.Principal.WindowsPrincipal下的WindowsPrincipal类获取用户角色。
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(current);
if (principal.IsInRole("your_role_here")
{
Console.WriteLine("Is a member of your role");
}https://stackoverflow.com/questions/3106257
复制相似问题