我在asp.net mvc中使用FormsAuthentication记录登录用户的信息,然后用授权过滤器[Authorize(Roles="admin")]过滤非法用户的,
大概的代码如下:
public bool AdminLogOn(string account, string password, string userType)
{
bool flg = false;
if (ValidateStudent(account, password, userType))
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, account, DateTime.Now,
DateTime.Now.AddMinutes(20), false, userType);
string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(
FormsAuthentication.FormsCookieName, encTicket));
flg = true;
}
return flg;
}
[Authorize(Roles="admin")]
public class AdminController : Controller
{
//管理员主页
public ActionResult Index()
{。。。。}
}
但现在的项目是用asp.net web form 来做的,
是怎么用权限过滤器的???
比如我的学生页面要是管理员登录之后,才能看到信息,那么我在page load函数里面写了:
if (User.Identity.IsAuthenticated)
{
if (!IsPostBack)
{
string name = this.TextBoxName.Text;
string no = this.TextBoxNo.Text;
BindRepeater(name, no);
}
}
else
{
FormsAuthentication.RedirectToLoginPage();
}
我支配的了他是否登录, 但是我也怎么判断他的权限(role)是“admin”呢??
各位大侠是用什么方法记录登录者的信息的???
是不是最简单用session来记录???
比如:
User user =new User{ Account="aaa" , Role="admin" } ;
Session["user"] = user ;
//在其他页面
User us = Session["user"] as User ;
if( user.Role=="admin" ){。。。。。}
各位大侠是用什么方法记录登录者的信息的???
主要是本人对web form 不太熟