这两天在看forms身份验证知识时遇到一个问题,很是疑惑。
   例如,我再站点根目录下的web.config的定义如下(默认的定义没贴上):
                   <authentication mode="Forms">
      <forms name=".ASPXAUTX" loginUrl="./login.aspx" timeout="30" path="/">
      </forms>
      <!--
      name即是身份验证发送到客户端的cookie的名字,loginUrl指的是当用户没有经过身份验证时所要进入的页面
      -->
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization> 然后我自己写的身份验证票据,代码如下:
       string user = TextBox1.Text;
        string password = TextBox2.Text;        if (confirm(user, password) == true)
        {
            string userRoles = userToRole(user);
            //建立身份验证票对象
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user, DateTime.Now, DateTime.Now.AddMinutes(30), false, userRoles, "/");
            //将验证票加密
            string hashTicket = FormsAuthentication.Encrypt(ticket);            //生成cookie
            /*System.Web.HttpCookie类,对应集合类System.Web.HttpCookieCollection类
                  System.Net.Cookie类,对应集合类System.Net.CookieCollection类*/            HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);            //输出cookie
            Response.Cookies.Add(userCookie);            //FormsAuthentication.SetAuthCookie(登陆名, false); 
           /* string url = Request["ReturnUrl"];
            string urls = url.Substring(7,18);
            */         //Response.Redirect(urls);
            //string url = FormsAuthentication.GetRedirectUrl(TextBox1.Text,false);
            //Response.Redirect(url);
           
            //  将已验证身份的用户重定向回最初请求的URL,但像以上的两种方法都不行,好像是response.Redirect方法不进行身份验证
            //RedirectFromLoginPage是封装了传登录信息,必须要是 mode="Forms"才能传,Response.Redirect转页
           FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);   
[/color]        }
        else
        {
            Response.Write("你的身份不能够请求此页!");
        }
    }    private bool confirm(string user,string password)
    {
        if (user == "derray" && password == "123456")
        { return true; }
        if (user == "hello" && password == "123456")
        { return true; }
        else
            return false;
    }    private string userToRole(string user)
    {
        string users="derray,admin,liming";
        return users;
    }
票据代码是在login.aspx页面中点击登录按钮后产生的,最后我在global.asax的页面中定义 Application_AuthenticateRequest事件来把用户和角色绑定,代码如下:
     protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpApplication app=(HttpApplication)sender;
        HttpContext context=app.Context;
        //验证过的用户才进行role的处理
        if (context.Request.IsAuthenticated == true)
        {
            FormsIdentity id = (FormsIdentity)context.User.Identity;            FormsAuthenticationTicket ticket = id.Ticket;
            string[] userRole = ticket.UserData.Split(',');
                      context.User = new System.Security.Principal.GenericPrincipal(id, userRole);
            
        }
              
    }
这时问题就出来啦,为什么我访问default.aspx页面时,如果将
上面的FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false); 换成
Response.Redirect(FormsAuthentication.GetRedirectUrl(TextBox1.Text,false));后就不能访问default.aspx页面呢?
如果用FormsAuthentication.RedirectFromLoginPage方法可以成功跳转到default页面,但上面设置的角色一点都没有用呢?因为我在default.aspx页面中的
 protected void Page_Load(object sender, EventArgs e)
    {
            if (User.Identity.IsAuthenticated)
        {
            Response.Write(HttpContext.Current.User.IsInRole("admin"));
        }
    }
事件中总是得到false,
希望哪位好友帮小弟解决一下啊,实在是想不通啊!!  

解决方案 »

  1.   

    你上面已经将FormsAuthenticationTicket 写入cookie了,就不用再写RedirectFromLoginPage了,RedirectFromLoginPage封装了包括写入cookie,跳转等一系列动作(不包含角色设定)。不过奇怪的是为什么你就不可以跳转呢。我前天刚写过这类文章,那儿我测试性的啊!
    http://www.cnblogs.com/qianlifeng/archive/2010/12/03/1895801.html
      

  2.   

    我用response.redirect()跳转就不行啊,我不知道这个response.redirect是不是又重新向你原来请求的页面发送过请求的啊?如果是这样 这次我已经写啦cookie和票据 就应该有权限查看我原先的页面啦吧?
    为什么我的不行呢?
      

  3.   

    if (验证成功)
    {
         if (FormsAuthentication.GetRedirectUrl(dataReader["userID"].ToString(), false).ToLower().EndsWith("default.aspx"))
         {
        FormsAuthentication.SetAuthCookie(dataReader["userID"].ToString(), false); 
                Response.Redirect("MainWeb.aspx");
         }
         else
         {
                FormsAuthentication.RedirectFromLoginPage(dataReader["userID"].ToString(), false);
         }
    }