我有一个网络项目GUI ..
我第一次只与管理员合作。
因此,当管理员使用他的用户名和密码登录时,我使用表单身份验证将其重定向到默认页面“Default.aspx”。
但现在我必须与宾客一起工作......并在登录时
检查角色是否属于访客,然后将其重定向到访客页面而不是“Default.aspx”
具有只读权限...例如,即使有选项,他也不应该对数据库进行任何更改
我正在使用此代码:
public partial class Login : 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)
{
if (impersonateValidUser(UserName.Text, Domain.Text, Password.Text) == true)
{
Label1.Text = "impersonation";
}
else
{
Label2.Text = "not impersonating";
}
//impersonateValidUser(UserName.Text, Domain.Text, Password.Text);
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 if(wp.IsInRole("Guest"))
{
?? I want to redirect it to the guestpage.aspx and not the default.aspx
}
}
else
{
BadCredentials.Visible = true;
Label4.Text = "not valid user";
}
}
private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
这对我来说非常重要......任何建议都将受到赞赏..谢谢
在SQL或IIS中是否存在一些针对Guest ????
的只读模式我在我的webconfig中使用过它
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" name="Cookie" timeout="120" path="/">
</forms>
</authentication>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
这有效..
答案 0 :(得分:2)
要处理重定向问题,您只需自己创建表单身份验证票证,然后执行Response.Redirect而不是使用内置的RedirectFromLoginPage方法。
请看这里的步骤7-10: http://msdn.microsoft.com/en-us/library/aa302399.aspx
就安全授权问题而言,您应该使用User.IsInRole方法在应用程序中启用/禁用功能,以防止用户做他们不应该做的事情。如果安全性不够,那么可以考虑为每个应用程序卷提供不同的Sql连接/ Sql用户/角色。但这可能有点过头了。
答案 1 :(得分:2)
您在进行表单身份验证还是Windows身份验证?以上看起来像Windows身份验证(即主机正在验证用户)。表单身份验证可以针对您想要的任何内容(例如数据库等)完成。
如果要管理用户(例如在数据库中),则需要设计这些机制。看看Membership Provider。您还可以尝试将用户登录到Windows计算机(或域),如果失败则回退到使用您自己的数据库等。