自定义身份验证不适用于asp.net 2.0

时间:2010-05-11 13:09:04

标签: asp.net asp.net-mvc-2 asp.net-membership

我正在尝试升级具有自定义书面登录的mvc 1.0应用程序。我像这样指定authcookie:

string _roles = string.Join(",", _ugr.GetUsergroupRoles(_username));

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
      1,
      _username,  
      DateTime.Now,
      DateTime.Now.AddHours(1),  
      false,  
      _roles,
      "/");

 HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
            HttpContext.Response.Cookies.Add(cookie);

当我调试时,我得到了_roles =“Admin”

我有一个动作过滤器,它覆盖OnExecuting我所拥有的地方:

..
string[] _authRoles = AuthRoles.Split(',');

bool isAuthorized = _authRoles.Any(r => filterContext.HttpContext.User.IsInRole(r));

if (!isAuthorized)
{
 ..

如果我调试_authRoles,其中包含“Admin”,并且isAuthorized始终为false。

如果我检查“票证”,它有一些:UserData =“Admin”。

那里有什么不对?是“User.IsInRole”是不同的,还是我需要在web.config中添加一些内容?

/ M