asp.net,角色未通过表单身份验证分配
又一天遇到有关 ASP.NET 角色的问题。所以我目前正在尝试为我的网站设置角色。我使用这段代码来制作 formsAuthentication 票证。
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, //Ticket version
p.firstName, //username
DateTime.Now,
DateTime.Now.AddMinutes(30),
false, //true for persistant user cookie
"Admin",
FormsAuthentication.FormsCookiePath);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx");
您可以看到我正在向此人添加“admin”角色。 p。是一个对象,名字是一个字符串。在下一段代码中,我检查该人在进入管理站点时是否获得了管理员角色。如果他不是管理员,则必须将其退回。
if (User.IsInRole("Admin") != true)
{
Response.Redirect("Default.aspx");
}
奇怪的是它让我返回默认页面?有人知道答案吗?
附言。我知道您必须在 web.config 文件的不同位置设置授权。但如果他连这个角色都拿不到,那现在也没关系。
Another day with problems concerning roles in asp.net. So I'm currently trying to set up roles for my website. I use this piece of code for making the formsAuthentication ticket.
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, //Ticket version
p.firstName, //username
DateTime.Now,
DateTime.Now.AddMinutes(30),
false, //true for persistant user cookie
"Admin",
FormsAuthentication.FormsCookiePath);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx");
You can see that I'm adding the role "admin" to this person. p. is an object and firstname is a string. In the next piece of code I check wether the person got the admin role when he enters an admin site. if he's not an admin he has to be returned.
if (User.IsInRole("Admin") != true)
{
Response.Redirect("Default.aspx");
}
the odd thing is that it returns me to the default page? anyone knows the answer?
PS. I know you have to set up authorization in the different locations in the web.config file. But if he doesn't even get the role it doesn't matter for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你能否让角色以这种方式工作。它的设计工作方式是通过角色管理器。有关详细信息,请参阅本文。
您可能需要查看的一件事是
cacheRolesInCookie
设置。如果禁用该功能,您可以确保默认的表单身份验证内容不会从身份验证 cookie 重新加载角色。I'm not sure you'll be able to get roles to work this way. The way it was designed to work is through the role manager. See this article for maore details.
One thing you may want to look at is the
cacheRolesInCookie
setting. If that is disabled, you can be sure that the default forms authentication stuff will not reload the roles from the authentication cookie.