ASP.. NET WebForms -如何授权访问页面
本文关键字:授权 访问 WebForms NET 何授权 ASP | 更新日期: 2023-09-27 18:02:06
在最新的ASP。. NET WebForms应用程序,我们不再使用RoleManager等(据我所知),所以我们如何授权访问一个特定角色的网页?
在MVC中,我会使用授权属性,但在WebForms中不存在,所以我在一个损失-任何想法?
考虑使用/a web。配置文件和授权元素。你可以创建一个网络。为此目的,您可以在任何目录下设置多个web. Config文件。
一个链接(查看其他链接):https://msdn.microsoft.com/en-us/library/8d82143t%28v=vs.85%29.aspx
试试这段代码在登录时将角色传递给FormsAuthenticationTicket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName.Text, DateTime.Now, DateTime.Now.AddMinutes(2880), false, role, FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
Response.Cookies.Add(cookie);
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false));
在Page_Load事件中检索角色
protected void Page_Load(object sender, EventArgs e)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string userData = ticket.UserData;
string[] temp = userData.Split(',');
role=temp[0];
if (role!="Owner")
{
Response.Write("............");
}
}
如果您想要文件夹级别的授权,那么请指定web中的角色,而不是检查web表单上的角色。该文件夹的配置文件
<authorization>
<allow roles="Owner"/>
<deny users="*"/>
</authorization>