c#新手—form身份验证问题
项目中有两个文件夹admin和user,同级目录下有个login.aspx登陆页面
主配置文件:
<authentication mode="Forms">
<forms loginUrl="login.aspx" name="ckName"></forms>
</authentication>
<authorization>
<deny users="*"/>
</authorization>
admin文件夹下配置文件:
<system.web>
<authorization>
<allow users="admin"/>
<deny users="*"/>
</authorization>
</system.web>
user文件夹下配置文件:
<system.web>
<authorization>
<allow users="user"/>
<deny users="*"/>
</authorization>
</system.web>
登陆按钮后台代码:
if (txt_name.Text == "user" && txt_pwd.Text == "123")
{
FormsAuthenticationTicket tk = new FormsAuthenticationTicket(1, FormsAuthentication.FormsCookieName, DateTime.Now, check_rem.Checked ? DateTime.Now.AddDays(30) : DateTime.Now.AddDays(1), false, "user");
string tkStr = FormsAuthentication.Encrypt(tk);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,tkStr);
cookie.Path = @"D:\";
Response.Cookies.Add(cookie);
Response.Redirect("user/user.aspx");
}
else if (txt_name.Text == "admin" && txt_pwd.Text == "123")
{
FormsAuthenticationTicket tk = new FormsAuthenticationTicket(1,FormsAuthentication.FormsCookieName, DateTime.Now, check_rem.Checked ? DateTime.Now.AddDays(30) : DateTime.Now.AddDays(1), false, "admin");
string tkStr = FormsAuthentication.Encrypt(tk);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, tkStr);
Response.Cookies.Add(cookie);
Response.Redirect("admin/admin.aspx");
}
全局文件:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.IsAuthenticated)
{
HttpCookie cookie = HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName];
[color=#FF0000]FormsAuthenticationTicket tk = FormsAuthentication.Decrypt(cookie.Value);[/color
string[] users = tk.UserData.Split(new char[] { '|' });
FormsIdentity identity = new FormsIdentity(tk);
HttpContext.Current.User = new GenericPrincipal(identity,users);
}
}
每次当执行到下划线语句的时候,就会报错![Encrypt()内的参数无效]
调试时确实发现cookie.value的值为null, cookie名称又没有错,在登陆的时候添加cookie的时候用的是那个名称[FormsAuthentication.FormsCookieName]啊,value值[FormsAuthentication.Encrypt(tk)]明明也存在啊
为什么会出现这种情况啊? 向高手求救 跪求!!!!