我公司開發的Web應用程序,只有用戶具有管理員角色可以訪問它並沒有其他人..檢查管理員
我有困難想弄清楚,如果用戶是管理員或不////
我的登錄頁面代碼是
public partial class LoginPage : System.Web.UI.Page
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
//WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
protected void LoginButton_Click(object sender, EventArgs e)
{
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if(LogonUserA(UserName.Text, Domain.Text, Password.Text, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal wp = new System.Security.Principal.WindowsPrincipal(wi);
if (wp.IsInRole("Administrators"))
{
BadCredentials.Visible = false;
Session["userName"] = UserName.Text;
Session["password"] = Password.Text;
Session["domain"] = Domain.Text;
FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
}
else
{
BadCredentials.Visible = true;
Label1.Text = wp.Identity.ToString();
Label2.Text = wi.Name.ToString();
Label3.Text = "not Admin";
}
}
else
{
BadCredentials.Visible = true;
Label4.Text = "not valid user";
}
當我把管理員和密碼
在此Label2.text給了我
XYZCOMP\IUSR_XYZCOMP
,不進入下一個頁面....
任何幫助,請...感謝
我使用窗體身份驗證
> <authentication mode="Forms"> <forms
> name="AuthCookie"
> LoginUrl="Login.aspx" timeout = "60"
> /> </authentication> <authorization>
> <deny users="?" />
> <allow users="*" />
> </authorization>
我得到一個錯誤: 無法投類型System.Security.Principal.GenericIdentity的對象鍵入System.Security.Principal.WindowsIdentity – user175084 2009-11-18 19:21:51
我改了行 System.Security.Principal.WindowsIdentity無線=系統。 Security.Principal.WindowsIdentity.GetCurrent(); 到 System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); – user175084 2009-11-18 19:23:22