作为未经验证的性感技术的贪婪,我在我的Web窗体应用程序中采用System.Web.Routing
来管理导航等。此外,我希望将基于角色的安全性从web.config移动到路由定义本身,因此我可以说“此路由仅适用于角色x,y”。
所以我得到了实现IRouteHandler的类,在它尝试加载特定页面之前,它会检查用户是否在其允许的角色集中。我的问题是,如果不是,我如何重定向到该处理程序中的登录页面?我知道可以在该实例中加载登录页面,但我更喜欢使用“returnto”页面进行干净的重定向。
public IHttpHandler GetHttpHandler(RequestContext requestContext) {
if ( AllowedRoles != null )
{
bool allowed = false;
for ( int i = 0; i < AllowedRoles.Length; i++ )
{
if ( requestContext.HttpContext.User.IsInRole( AllowedRoles[i] ) )
{
allowed = true;
break;
}
}
if ( !allowed )
{
???
}
}
答案 0 :(得分:4)
可以从GetHttpHandler进行重定向。只需使用:
requestContext.HttpContext.Response.Redirect("login.aspx");