这两天在看forms身份验证知识时遇到一个问题,很是疑惑。
例如,我再站点根目录下的web.config的定义如下(默认的定义没贴上):
<authentication mode="Forms">
<forms name=".ASPXAUTX" loginUrl="./login.aspx" timeout="30" path="/">
</forms>
<!--
name即是身份验证发送到客户端的cookie的名字,loginUrl指的是当用户没有经过身份验证时所要进入的页面
-->
</authentication>
<authorization>
<deny users="?" />
</authorization> 然后我自己写的身份验证票据,代码如下:
string user = TextBox1.Text;
string password = TextBox2.Text; if (confirm(user, password) == true)
{
string userRoles = userToRole(user);
//建立身份验证票对象
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user, DateTime.Now, DateTime.Now.AddMinutes(30), false, userRoles, "/");
//将验证票加密
string hashTicket = FormsAuthentication.Encrypt(ticket); //生成cookie
/*System.Web.HttpCookie类,对应集合类System.Web.HttpCookieCollection类
System.Net.Cookie类,对应集合类System.Net.CookieCollection类*/ HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket); //输出cookie
Response.Cookies.Add(userCookie); //FormsAuthentication.SetAuthCookie(登陆名, false);
/* string url = Request["ReturnUrl"];
string urls = url.Substring(7,18);
*/ //Response.Redirect(urls);
//string url = FormsAuthentication.GetRedirectUrl(TextBox1.Text,false);
//Response.Redirect(url);
// 将已验证身份的用户重定向回最初请求的URL,但像以上的两种方法都不行,好像是response.Redirect方法不进行身份验证
//RedirectFromLoginPage是封装了传登录信息,必须要是 mode="Forms"才能传,Response.Redirect转页
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false); [/color] }
else
{
Response.Write("你的身份不能够请求此页!");
}
} private bool confirm(string user,string password)
{
if (user == "derray" && password == "123456")
{ return true; }
if (user == "hello" && password == "123456")
{ return true; }
else
return false;
} private string userToRole(string user)
{
string users="derray,admin,liming";
return users;
}
票据代码是在login.aspx页面中点击登录按钮后产生的,最后我在global.asax的页面中定义 Application_AuthenticateRequest事件来把用户和角色绑定,代码如下:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpApplication app=(HttpApplication)sender;
HttpContext context=app.Context;
//验证过的用户才进行role的处理
if (context.Request.IsAuthenticated == true)
{
FormsIdentity id = (FormsIdentity)context.User.Identity; FormsAuthenticationTicket ticket = id.Ticket;
string[] userRole = ticket.UserData.Split(',');
context.User = new System.Security.Principal.GenericPrincipal(id, userRole);
}
}
这时问题就出来啦,为什么我访问default.aspx页面时,如果将
上面的FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false); 换成
Response.Redirect(FormsAuthentication.GetRedirectUrl(TextBox1.Text,false));后就不能访问default.aspx页面呢?
如果用FormsAuthentication.RedirectFromLoginPage方法可以成功跳转到default页面,但上面设置的角色一点都没有用呢?因为我在default.aspx页面中的
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
Response.Write(HttpContext.Current.User.IsInRole("admin"));
}
}
事件中总是得到false,
希望哪位好友帮小弟解决一下啊,实在是想不通啊!!
例如,我再站点根目录下的web.config的定义如下(默认的定义没贴上):
<authentication mode="Forms">
<forms name=".ASPXAUTX" loginUrl="./login.aspx" timeout="30" path="/">
</forms>
<!--
name即是身份验证发送到客户端的cookie的名字,loginUrl指的是当用户没有经过身份验证时所要进入的页面
-->
</authentication>
<authorization>
<deny users="?" />
</authorization> 然后我自己写的身份验证票据,代码如下:
string user = TextBox1.Text;
string password = TextBox2.Text; if (confirm(user, password) == true)
{
string userRoles = userToRole(user);
//建立身份验证票对象
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user, DateTime.Now, DateTime.Now.AddMinutes(30), false, userRoles, "/");
//将验证票加密
string hashTicket = FormsAuthentication.Encrypt(ticket); //生成cookie
/*System.Web.HttpCookie类,对应集合类System.Web.HttpCookieCollection类
System.Net.Cookie类,对应集合类System.Net.CookieCollection类*/ HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket); //输出cookie
Response.Cookies.Add(userCookie); //FormsAuthentication.SetAuthCookie(登陆名, false);
/* string url = Request["ReturnUrl"];
string urls = url.Substring(7,18);
*/ //Response.Redirect(urls);
//string url = FormsAuthentication.GetRedirectUrl(TextBox1.Text,false);
//Response.Redirect(url);
// 将已验证身份的用户重定向回最初请求的URL,但像以上的两种方法都不行,好像是response.Redirect方法不进行身份验证
//RedirectFromLoginPage是封装了传登录信息,必须要是 mode="Forms"才能传,Response.Redirect转页
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false); [/color] }
else
{
Response.Write("你的身份不能够请求此页!");
}
} private bool confirm(string user,string password)
{
if (user == "derray" && password == "123456")
{ return true; }
if (user == "hello" && password == "123456")
{ return true; }
else
return false;
} private string userToRole(string user)
{
string users="derray,admin,liming";
return users;
}
票据代码是在login.aspx页面中点击登录按钮后产生的,最后我在global.asax的页面中定义 Application_AuthenticateRequest事件来把用户和角色绑定,代码如下:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpApplication app=(HttpApplication)sender;
HttpContext context=app.Context;
//验证过的用户才进行role的处理
if (context.Request.IsAuthenticated == true)
{
FormsIdentity id = (FormsIdentity)context.User.Identity; FormsAuthenticationTicket ticket = id.Ticket;
string[] userRole = ticket.UserData.Split(',');
context.User = new System.Security.Principal.GenericPrincipal(id, userRole);
}
}
这时问题就出来啦,为什么我访问default.aspx页面时,如果将
上面的FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false); 换成
Response.Redirect(FormsAuthentication.GetRedirectUrl(TextBox1.Text,false));后就不能访问default.aspx页面呢?
如果用FormsAuthentication.RedirectFromLoginPage方法可以成功跳转到default页面,但上面设置的角色一点都没有用呢?因为我在default.aspx页面中的
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
Response.Write(HttpContext.Current.User.IsInRole("admin"));
}
}
事件中总是得到false,
希望哪位好友帮小弟解决一下啊,实在是想不通啊!!
解决方案 »
- 帮我看一下代码中的“rdlog["sys_power"].ToString()”这一句应该怎么改才可以正常运行
- 如何实现上传文件图片而不刷新页面?
- 问个DateTime和NULL的问题?
- html控件上传图片的后台代码如何写
- 关于在新页面的打开!
- 一个动太态更新菜单问题-----在线等!!!
- 一个.net编译过程中常见的问题,问了几次,似乎没好办法解决
- 在windows forms 中,增样在一个form窗口中,打开另外一个窗口。我是在windows application 的窗口里打开另一个桌面窗口
- webform1.aspx.65e33e68.compiled 這是什麼文件,起什麼作用??
- 请问一个IE页面最大的显示容量是多少?
- 聊天室问题
- asp.net MVC 按钮添加链接 急急急 谢谢
http://www.cnblogs.com/qianlifeng/archive/2010/12/03/1895801.html
为什么我的不行呢?
{
if (FormsAuthentication.GetRedirectUrl(dataReader["userID"].ToString(), false).ToLower().EndsWith("default.aspx"))
{
FormsAuthentication.SetAuthCookie(dataReader["userID"].ToString(), false);
Response.Redirect("MainWeb.aspx");
}
else
{
FormsAuthentication.RedirectFromLoginPage(dataReader["userID"].ToString(), false);
}
}