登录时string userName = "zhang";
string userRole = "admin";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
userRole,
"/");
string hashTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
Response.Cookies.Add(cookie);
Application_AuthorizeRequest(object sender, System.EventArgs e)时HttpApplication App = (HttpApplication)sender;
HttpContext Ctx = App.Context; //获取本次Http请求相关的HttpContext对象
if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理
{
FormsIdentity Id = (FormsIdentity)Ctx.User.Identity;
FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票
string[] Roles = Ticket.UserData.Split(','); //将身份验证票中的role数据转成字符串数组
Ctx.User = new GenericPrincipal(Id, Roles); //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息
}Web.config<authentication mode="Forms" >
<forms name="DemoAuth" loginUrl="Web/login.aspx" timeout="30" path="/" />
</authentication>
<authorization>
<deny users="?"/>
</authorization><location path="Demo1">
<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>跳转时//在这里roleOk已经是true了,为什么还是跳不进去,郁闷!待解!急!
bool roleOk = HttpContext.Current.User.IsInRole("admin");
Response.Redirect("Demo1/demo1.aspx");
高手指教!是不是Web.config设置的不对?
string userRole = "admin";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
userRole,
"/");
string hashTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
Response.Cookies.Add(cookie);
Application_AuthorizeRequest(object sender, System.EventArgs e)时HttpApplication App = (HttpApplication)sender;
HttpContext Ctx = App.Context; //获取本次Http请求相关的HttpContext对象
if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理
{
FormsIdentity Id = (FormsIdentity)Ctx.User.Identity;
FormsAuthenticationTicket Ticket = Id.Ticket; //取得身份验证票
string[] Roles = Ticket.UserData.Split(','); //将身份验证票中的role数据转成字符串数组
Ctx.User = new GenericPrincipal(Id, Roles); //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息
}Web.config<authentication mode="Forms" >
<forms name="DemoAuth" loginUrl="Web/login.aspx" timeout="30" path="/" />
</authentication>
<authorization>
<deny users="?"/>
</authorization><location path="Demo1">
<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>跳转时//在这里roleOk已经是true了,为什么还是跳不进去,郁闷!待解!急!
bool roleOk = HttpContext.Current.User.IsInRole("admin");
Response.Redirect("Demo1/demo1.aspx");
高手指教!是不是Web.config设置的不对?
解决方案 »
- 为什么只有一个构造函数?只有这几分了,请大家帮忙
- vs2010beta2 打开beta1项目的问题
- 高手进,如何提高大并发量的系统性能
- ASP.NET GRIDVIEW 分页问题(代码多,题简单,新手问题)
- 来看看这个问题!!!
- 我的毕业设计项目课题,很有挑战性的项目 ,是关于客户在线产品设计的一个系统,技术上有困难,帮帮我!!!!!!!!!!
- 怎么在打印之前用代码进行页面设置???
- ASP.NET中的SQL SERVER联接问题!
- ObjectDataSource问题!
- 请问xp professional的SMTP是怎么生效的?
- DataGrid 更新某一行数据报错!
- 100分求:System.ArgumentException: Column 'xxx' does not belong to table Table.
<allow roles="admin"/>
<deny users="*"/>
至于结果吗,就不好说了。。
我知道“roleOk无论是什么值都会跳转的”,加个bool roleOk = HttpContext.Current.User.IsInRole("admin")只是为了让看帖的人明白。我的意思是既然当前user的角色是admin,那为什么还进不了"Demo1/demo1.aspx"。如果谁知道是Web.config有问题,那请写出正确的,不要说句“web.config”有问题就走人!!
<authorization>
<deny users="?"/>
</authorization>为
<authorization>
<allow users="*"/>
</authorization>
参考