If red.Read() Then                    Dim tc As FormsAuthenticationTicket
                    tc = New FormsAuthenticationTicket(1, red.GetValue(0).ToString, DateTime.Now, DateTime.Now.AddDays(1), False, "student")
                    Dim encrypt As String = FormsAuthentication.Encrypt(tc)
                    Dim ck As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, encrypt)
                    ck.Expires = DateTime.Now.AddDays(1)
                    Response.Cookies.Add(ck)
                    Dim ru As String = FormsAuthentication.GetRedirectUrl(FormsAuthentication.FormsCookieName, False)
                   Response.Redirect(ru)                    Label1.Text = User.Identity.Name &  User.IsInRole("student")
                Else
                    Label1.Text = "error1"
                End If
问题是我看别人的资料是FormsAuthenticationTicket(1, red.GetValue(0).ToString, DateTime.Now, DateTime.Now.AddDays(1), False, "student") 里面的"student"是角色 
可我在做的时候 User.IsInRole 是False 但是  User.Identity.Name  的植 又可以取到 
该怎么坚决啊

解决方案 »

  1.   

    奇怪,你在 Label1.Text 之前都已经 Response.Redirect(ru)
    了,   Label1.Text = User.Identity.Name &  User.IsInRole("student")
    这句话根本就不会执行。。把验证票放在CooKie,还要通过
    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
      {
     string cookieName = FormsAuthentication.FormsCookieName;
     HttpCookie authCookie = Context.Request.Cookies[cookieName];
     FormsAuthenticationTicket authTicket = null;
     try
     {
         authTicket = FormsAuthentication.Decrypt(authCookie.Value);
     }
     catch(Exception ex)
     {
         return;
     }
     
     string[] roles = authTicket.UserData.Split(new char[]{','});//如果存取多个角色,我们把它分解
     
     FormsIdentity id = new FormsIdentity( authTicket ); 
     
     GenericPrincipal principal = new GenericPrincipal(id, roles);
     Context.User =principal;//存到HttpContext.User中 
     
      }
    把角色取出来
    http://www.hubro.net/html_OS-Gdo9vh3Q.ashx
      

  2.   

    可是我的是2.0的版本啊  在说哪个是C#的 我改过但是在Global.asax  根本就不对
    我估计那时1.X版本用的
      

  3.   

    2.0跟1.1这个基本上没差别吧。可我在做的时候 User.IsInRole 是False 但是 User.Identity.Name 的植 又可以取到 跟阿亮所说一样。你进行Form验证的时候还需要Application_AuthenticateRequest
      

  4.   

    有谁可以告诉我Form验证的时候还需要Application_AuthenticateRequest 里面该怎么写吗
    我看别的写的都 引进了空间 可我在写的时候2.0里根本无法引进空间
    <%@ imports System.Security.Principal%>
    <%@ Imports System.Web.Security %>
    Principal不是Application的有效属性  大家帮帮忙 提供点VB的好吗
      

  5.   

    <%@ import   不是imports
      

  6.   

    <%@ Import System.Security.Principal %>Principal不具有Import属性 
    这也抱错了 
      

  7.   

    问题坚决了 
    现在公布坚决方法但不是最佳的 因为在不加TRY的时候 等出会报错 
    其他的不变 主要是Global.asax  的内容要改 我改了两个 随便那一个都可以 特别感谢啊亮Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
            If Not (HttpContext.Current.User Is DBNull.Value) Then
                Try
                    If (HttpContext.Current.User.Identity.IsAuthenticated) Then
                        If (HttpContext.Current.User.Identity.AuthenticationType = "Forms") Then
                            Dim fi As FormsIdentity = HttpContext.Current.User.Identity
                            Dim ticket As FormsAuthenticationTicket = fi.Ticket
                            Dim userData As String = ticket.UserData
                            Dim roles As String() = userData.Split(",")
                            HttpContext.Current.User = New System.Security.Principal.GenericPrincipal(fi, roles)
                        End If
                    End If
                Catch ex As Exception
                    Return
                End Try
            End If
        End Sub或者
     Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
            Dim cookieName As String = FormsAuthentication.FormsCookieName
            Dim authCookie As HttpCookie = Context.Request.Cookies(cookieName)
            Dim authTicket As FormsAuthenticationTicket
            Try
                authTicket = FormsAuthentication.Decrypt(authCookie.Value)
            Catch ex As Exception
                Return
            End Try
            Dim roles As String() = authTicket.UserData.Split("'")
            Dim id As FormsIdentity = New FormsIdentity(authTicket)
            Dim principal As System.Security.Principal.GenericPrincipal = New System.Security.Principal.GenericPrincipal(id, Roles)
            Context.User = principal
        End Sub