我无法让Authorize
属性与角色一起使用。这就是我装饰控制器的方式:
[Authorize(Roles = "admin")]
public ActionResult Index()
{
...
}
这就是我登录用户的方式:
string roles = "admin";
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
username,
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
roles
);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
HttpContext.Current.Response.Cookies.Add(cookie);
但是我的用户仍然被拒绝访问。我哪里错了?