如何使 .NET WebForm 路由与授权一起工作

发布于 2024-10-08 16:35:20 字数 602 浏览 0 评论 0原文

我有从数据库注册到 asp.net 网站(非 MVC)的路由。路线注册正常,当我登录时它们都可以工作。我想做的是根据一些路线数据创建一个登陆页面:

页面是 [site]/landing/dell

路线看起来像:“landing/{client }”,它路由到我的页面 Login.aspx,在那里我让客户端退出路由,然后根据值显示一些自定义品牌数据。

在我的 web.config 中,我将身份验证模式设置为表单,我的 loginUrl = "Login.aspx"

当用户没有授权 cookie 时,它​​会将用户重定向到:

[site]/Login.aspx?ReturnUrl= %2flanding%2fdell 而不是保留路由 url,并显示正确的数据。 IIS 服务器实际上根本不处理路由,只是将用户发送到 Login.aspx 页面。

我已经尝试在 web.config 中添加一些内容: <位置路径=“登陆”><授权><允许用户=“*”/> ; 等等,还有很多变体,但似乎没有任何效果。

有人有想法吗?我认为这是一个常见问题,只是没有很好的记录。

I have routes that are being registered from the database into an asp.net website (non MVC). The routes register fine, they all work when I am logged in. What I am trying to do is create a landing page based on some route data:

Page is [site]/landing/dell

The route looks like: "landing/{client}" and it routes to my page Login.aspx, in there I get the client out of the route, then display some custom brand data based on the value.

In my web.config, I have my authentication mode set to forms, with my loginUrl = "Login.aspx"

When the user does not have the authorization cookie, it redirects the user to:

[site]/Login.aspx?ReturnUrl=%2flanding%2fdell instead of keeping the route url, and displaying the correct data. The IIS server actually does not even process the route at all, just sends the user to the Login.aspx page.

I have tried several additions to my web.config:
<location path="landing"><system.web><authorization><allow users="*"/></auth></sys.web></loc> etc, and many variations, but nothing seems to work.

Ideas anyone? I assume this is a common issue, and it is just not well documented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-10-15 16:35:20

好吧,终于想通了。我将尝试以一种简单的形式解释这一点,希望有一天它能对其他人有所帮助。

这里要记住几件事,首先,它是一个非 MVC 应用程序,纯粹的 Web 表单。

在我的 Global.ascx 中,有一个方法:

    void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
        routes.Add(new Route("{service}.asmx/{*pathInfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.psd/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.js/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.jpg/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.gif/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("{resource}.css/{*pathinfo}", new StopRoutingHandler()));

        routes.RouteExistingFiles = false;

        Data.DataContext context = new Data.DataContext();

        var AppRoutes = (from r in context.SomeRouteTable
                         select r).ToList();

        foreach (var AppRoute in AppRoutes)
        {
            routes.MapPageRoute(AppRoute.RouteName,
                                AppRoute.RouteUrl,
                                AppRoute.PhysicalFile, false);
            // The important part is the "false" above.  It is the 
            // CheckPhysicalUrlAccess parameter.
        }
    }

现在,在 web.config 中,需要添加一个条目:

<location path="landing"><system.web><authorization><allow users="*"/></authorization></system.web></location>

在 Global.asax 文件内的 Application_Start void 中,只需调用 RegisterRoutes 函数:

this.RegisterRoutes(RouteTable.Routes);

重新启动服务器,然后完成。现在,您的登录页面路由将有效,但所有其他路由都将是安全的。如果您需要公开另一条路由,您所要做的就是使用allow users="*" 将路由的基本路径添加到您的system.web 授权部分。

其中的另一件事是能够将所有 javascript(js)、图像(psd、jpg、gif)、实际上任何静态文件从路由处理程序中取出,这将有望对其他人有所帮助。在整个网络上,对 .axd 和 .asmx 进行了解释,但是我从来没有找到在处理 Webforms 模型中的路由时如何忽略其他静态文件类型的位置。

我希望这对其他人有帮助,并节省他们我花在跟踪所有这些并亲自进行所有单元测试上的时间。

享受人们。

Ok, finally figured it out. I am going to try and explain this in a simple form, in hopes that it will one day help somebody else.

Several things here to remember, first, it is a non MVC app, purely webforms.

In my Global.ascx, there is a method:

    void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
        routes.Add(new Route("{service}.asmx/{*pathInfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.psd/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.js/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.jpg/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("*.gif/{*pathinfo}", new StopRoutingHandler()));
        routes.Add(new Route("{resource}.css/{*pathinfo}", new StopRoutingHandler()));

        routes.RouteExistingFiles = false;

        Data.DataContext context = new Data.DataContext();

        var AppRoutes = (from r in context.SomeRouteTable
                         select r).ToList();

        foreach (var AppRoute in AppRoutes)
        {
            routes.MapPageRoute(AppRoute.RouteName,
                                AppRoute.RouteUrl,
                                AppRoute.PhysicalFile, false);
            // The important part is the "false" above.  It is the 
            // CheckPhysicalUrlAccess parameter.
        }
    }

Now, in the web.config, an entry needs to be added:

<location path="landing"><system.web><authorization><allow users="*"/></authorization></system.web></location>

In the Application_Start void inside of the Global.asax file simply call the RegisterRoutes function:

this.RegisterRoutes(RouteTable.Routes);

Restart your server, and done. Now your route for the login page will work, however all of your other routes will be secure. Should you need to expose another route all you have to do is add the base path of the route to your system.web authorization section with allow users="*".

The other thing that is in this, that will hopefully help others is the ability to take all of your javascript(js), images(psd, jpg, gif), really any static file out of the route handler. All over the web, the .axd and .asmx were explained, however it was never in a location I could find how to also do this ignore for other static file types when dealing with routing in a webforms model.

I hope this helps somebody else, and saves them the time I spent in tracking all of this down and doing all of the unit testing myself.

Enjoy folks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文