我在网上看了一段c#的代码,可是我只会一点点,vb.net,还请那位前辈有空帮我转换一下!谢谢!
1)
protected void Application_AuthorizeRequest(object sender, System.EventArgs e)
{
    HttpApplication App = (HttpApplication) sender;
     HttpContext Ctx = App.Context ; //获取本次Http请求相关的HttpContext对象
    if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理
    {
        FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ;
        FormsAuthenticationTicket Ticket = Id.Ticket ; //取得身份验证票
        string[] Roles = Ticket.UserData.Split (',') ; //将身份验证票中的role数据转成字符串数组
        Ctx.User = new GenericPrincipal (Id, Roles) ; //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息
    }
}
2)
 Context.Response.Redirect (Context.Request["ReturnUrl"]) ; // 重定向到用户申请的初始页面
 还请解释一下ReturnUrl是个什么参数呢?要怎么获取呢?谢谢各位前辈!

解决方案 »

  1.   

    http://www.developerfusion.com/utilities/convertcsharptovb.aspx这是C#转VB.NET的网站,你可以来这里转换程序
      

  2.   

    Protected Sub Application_AuthorizeRequest(ByVal sender As Object, ByVal e As System.EventArgs) 
     Dim App As HttpApplication = CType(sender, HttpApplication) 
     Dim Ctx As HttpContext = App.Context 
     If Ctx.Request.IsAuthenticated = True Then 
       Dim Id As FormsIdentity = CType(Ctx.User.Identity, FormsIdentity) 
       Dim Ticket As FormsAuthenticationTicket = Id.Ticket 
       Dim Roles As String() = Ticket.UserData.Split(",") 
       Ctx.User = New GenericPrincipal(Id, Roles) 
     End If 还请解释一下ReturnUrl是个什么参数呢?要怎么获取呢?
    ===========
    他是提交页面的参数..
    End Sub
    我给你翻译了下哈..
      

  3.   

    Protected Sub Application_AuthorizeRequest(ByVal sender As Object, ByVal e As System.EventArgs) 
     Dim App As HttpApplication = CType(sender, HttpApplication) 
     Dim Ctx As HttpContext = App.Context '获取本次Http请求相关的HttpContext对象
     If Ctx.Request.IsAuthenticated = True Then '验证过的用户才进行role的处理
       Dim Id As FormsIdentity = CType(Ctx.User.Identity, FormsIdentity) 
       Dim Ticket As FormsAuthenticationTicket = Id.Ticket '取得身份验证票
       Dim Roles As String() = Ticket.UserData.Split(",") '将身份验证票中的role数据转成字符串数组
       Ctx.User = New GenericPrincipal(Id, Roles) '将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息
     End If 
    End SubReturnUrl 是QueryString 参数,直接获取本页的地址付给ReturnUrl,用再登录等页面,如首页(default.aspx)登录连接 Login.aspx?ReturnUrl=Default.aspx 登录后则根据ReturnUrl跳转
      

  4.   

    啊!!谢谢!!谢谢!
    可是还有一点不明:
    2)中Login.aspx?ReturnUrl=Default.aspx 可以从login.aspx转到Default.aspx ,那如果要转到我刚刚的上一页呢?就是说来到Login.aspx之前的页面!谢谢
      

  5.   

    2)中Login.aspx?ReturnUrl=Default.aspx 可以从login.aspx转到Default.aspx ,那如果要转到我刚刚的上一页呢?就是说来到Login.aspx之前的页面!
    =========================
    那么你就response.redirect(request.querystring["ReturnUrl]);
      

  6.   


    这好像是c#的,(request.querystring["ReturnUrl])提示错误!
    谢谢前辈!
      

  7.   

    对不起!不是"刚刚的上一页呢" !
    应该是请求但需要权限(比如:gl.aspx),所以被跳过到login.aspx, 现在登陆成功了又想转到gl.aspx那Context.Response.Redirect 该如何写呢?
    谢谢各位前辈!
      

  8.   

    汗.我忘了你是用VB.NET的,恩,这样简单的方法..你在login.aspx的page_load事件中写dim strRequestUrl as string = Request.UrlReferrer.ToString然后在你验证登陆成功后,你再这样.response.redirect(strRequestUrl)这样就可以了
      

  9.   

    谢谢前辈们!
    我在附带问个问题:
    我用Ctx.User = New GenericPrincipal(Id, Roles) 将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息。
    //////////
    1)那我想得到当前正在连接的用户的名字和角色是不是该这样写: 
     Ctx.User.Identity 
     Ctx.User.IsInRole 2那Ctx是在global.asax中定义的,要在别的页面中使用该如何呢?谢谢前辈!(呵呵,vb代码)
      

  10.   

    1)
    User.Identity.name2)
    在别的页面不用了.你这个是在.asax文件或者是在类中是需要这样做。恩.
      

  11.   

    谢谢前辈,我用User.Identity.name到是看见了当前用户
    可是角色却看不见.name属性,那该如何看呢?谢谢!
      

  12.   

    Ctx.User.IsInRole     ''确定当前成员是否属于指定角色,象这个要自己去查SDK或者MSDN,都有if Ctx.User.IsInRole(User.Identity.name) then
     ....是的
    else
    ..不是
    end if
      

  13.   

    我用:
    Label3.Text = HttpContext.Current.User.IsInRole(User.Identity.Name)
    显示当前用户的角色,只能显示  false 或者 true 可是我给当前用户的cookei中写入的角色是“admin”,并且也是正确的,因为,已经能按不同的角色访问不同的目录了。现在我想知道的是怎样能在Label3中显示角色“admin”谢谢前辈!
      

  14.   

    user.Identity.AuthenticationType看看这样可以不,我不清楚.
      

  15.   

    应该不行,这个只能显示一个“Forms”
    谢谢前辈!
      

  16.   

    Dim myname as string = FormsAuthenticationTicket.Name这样看看,你这个我看了下SDK,好像没有什么直接的,你可以用间接的来.比如if Ctx.User.IsInRole("admin") then   ''检测当前用户是否为admin角色..
     ....是的
    else
    ..不是
    end if
      

  17.   

    我在Global.asax中的Application_AuthenticateRequest中用了此法,
    提示错误:
    未将对象引用设置到对象的实例。 谢谢前辈!
      

  18.   

    Protected Sub Application_AuthorizeRequest(ByVal sender As Object, ByVal e As System.EventArgs) 
     Dim App As HttpApplication = CType(sender, HttpApplication) 
     Dim Ctx As HttpContext = App.Context 
     If Ctx.Request.IsAuthenticated = True Then 
       Dim Id As FormsIdentity = CType(Ctx.User.Identity, FormsIdentity) 
       Dim Ticket As FormsAuthenticationTicket = Id.Ticket 
       Dim Roles As String() = Ticket.UserData.Split(","C) 
       Ctx.User = New GenericPrincipal(Id, Roles) 
     End If 
    End Sub
      

  19.   

    Context.Response.Redirect (Context.Request["ReturnUrl"]) 
    中的ReturnUrl是由QUERYSTRING传过来的地址参数