我在网上看了一段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)
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是个什么参数呢?要怎么获取呢?谢谢各位前辈!
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
我给你翻译了下哈..
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跳转
可是还有一点不明:
2)中Login.aspx?ReturnUrl=Default.aspx 可以从login.aspx转到Default.aspx ,那如果要转到我刚刚的上一页呢?就是说来到Login.aspx之前的页面!谢谢
=========================
那么你就response.redirect(request.querystring["ReturnUrl]);
这好像是c#的,(request.querystring["ReturnUrl])提示错误!
谢谢前辈!
应该是请求但需要权限(比如:gl.aspx),所以被跳过到login.aspx, 现在登陆成功了又想转到gl.aspx那Context.Response.Redirect 该如何写呢?
谢谢各位前辈!
我在附带问个问题:
我用Ctx.User = New GenericPrincipal(Id, Roles) 将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息。
//////////
1)那我想得到当前正在连接的用户的名字和角色是不是该这样写:
Ctx.User.Identity
Ctx.User.IsInRole 2那Ctx是在global.asax中定义的,要在别的页面中使用该如何呢?谢谢前辈!(呵呵,vb代码)
User.Identity.name2)
在别的页面不用了.你这个是在.asax文件或者是在类中是需要这样做。恩.
可是角色却看不见.name属性,那该如何看呢?谢谢!
....是的
else
..不是
end if
Label3.Text = HttpContext.Current.User.IsInRole(User.Identity.Name)
显示当前用户的角色,只能显示 false 或者 true 可是我给当前用户的cookei中写入的角色是“admin”,并且也是正确的,因为,已经能按不同的角色访问不同的目录了。现在我想知道的是怎样能在Label3中显示角色“admin”谢谢前辈!
谢谢前辈!
....是的
else
..不是
end if
提示错误:
未将对象引用设置到对象的实例。 谢谢前辈!
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
中的ReturnUrl是由QUERYSTRING传过来的地址参数