c#新手—form身份验证问题

ld490832353 2010-09-20 09:25:04
项目中有两个文件夹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)]明明也存在啊


为什么会出现这种情况啊? 向高手求救 跪求!!!!

...全文
194 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧