【问题标题】:Configuring Authorization in a mixed MVC / WebForms Web app在混合 MVC / WebForms Web 应用程序中配置授权
【发布时间】:2015-03-03 08:19:29
【问题描述】:

我目前正在将 WebForms / MVP 应用程序的一些组件迁移到 MVC 中。到目前为止,除了授权之外,一切正常。无论如何,当我导航到登录页面的 MVC 版本时,我会被重定向到在 Web.config 中设置的 aspx 页面:

    <authentication mode="Forms">
      <forms name=".MyWebSite" enableCrossAppRedirects="true" loginUrl="Login.aspx" timeout="60" path="/" defaultUrl="~/Pages/Landing.aspx"></forms>
    </authentication>

我尝试过使用AllowAnonymous,但似乎网络表单配置优先。这是我的登录控制器:

[RouteArea("User", AreaPrefix = "")]
public class AuthenticationController : Controller {
    [Route("Login")]
    [AllowAnonymous]
    public ActionResult Login() {
        return View();
    }
}

我的目录结构如下所示:

> Web Project
   > Areas
      > User 
          > Controllers
              > AuthController
          > Views
              > Login.cshtml

在我的 web.config 中,我看到以下允许匿名访问错误页面:

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

但是,为Areas 路径复制此路径不起作用(可能是因为 cshtml 文件实际上并不像 aspx 页面那样位于那里?)。

现在,如果我已登录(通过 aspx 版本的登录)并且我的用户已通过身份验证,我就可以正常访问 MVC 实现。路由和渲染工作得很好。它只是允许未经身份验证的用户访问 MVC 页面(而不重定向到 aspx 实现),这似乎是一个挑战。我究竟做错了什么?

编辑 我发现的一个非常老套的部分解决方案(基于Turning off ASP.Net WebForms authentication for one sub-directory)如下:

    protected void Application_BeginRequest(object sender, EventArgs e) {
        // lots of existing web.config controls for which webforms folders can be accessed
        // read the config and skip checks for pages that authorise anon users by having
        // <allow users="?" /> as the top rule.
        // https://stackoverflow.com/questions/4616524/turning-off-asp-net-webforms-authentication-for-one-sub-directory

        // check local config
        var localAuthSection = ConfigurationManager.GetSection("system.web/authorization") as AuthorizationSection;

        // this assumes that the first rule will be <allow users="?" />
        var localRule = localAuthSection.Rules[0];
        if (localRule.Action == AuthorizationRuleAction.Allow && localRule.Users.Contains("?")) {
            // then skip the rest
            return;
        }

        // get the web.config and check locations
        var conf = WebConfigurationManager.OpenWebConfiguration("~");
        foreach (ConfigurationLocation loc in conf.Locations) {
            // find whether we're in a location with overridden config

            // get page name
            var currentPath = Path.GetFileName(this.Request.Path);
            if (currentPath.Equals(loc.Path, StringComparison.OrdinalIgnoreCase)) {
                // get the location's config
                var locConf = loc.OpenConfiguration();
                var authSection = locConf.GetSection("system.web/authorization") as AuthorizationSection;
                if (authSection != null) {
                    // this assumes that the first rule will be <allow users="?" />
                    var rule = authSection.Rules[0];
                    if (rule.Action == AuthorizationRuleAction.Allow && rule.Users.Contains("?")) {
                        // then skip the rest
                        return;
                    }
                }
            }
        }
    }

这意味着我可以像这样指定“登录”:

  <location path="Login">
    <system.web>
      <authorization>
        <allow users="?" />
      </authorization>
    </system.web>
  </location>

但是所有相关的 CSS/JS 都不会被渲染,除非我为这些文件类型添加规则。 必须对此进行更优雅的解决。

【问题讨论】:

  • 我有一个混合环境,我都使用旧的 aspx 登录页面。无论用户如何通过身份验证,auth 标志都是相同的。是否有理由同时拥有 aspx 和 mvc 登录页面?
  • @tintyethan - 它被选为迁移到 MVC 的第一页,不幸的是我无法控制,除非这在技术上是不可能的。最终我们将停用 ASPX 实现,但 ASPX 身份验证需要保持原样。

标签: asp.net asp.net-mvc asp.net-mvc-4 webforms


【解决方案1】:

看看微软的名为“mvc 音乐商店”的教程。第 7 部分是关于身份验证和授权的配置。阅读 5-10 页,您现在就掌握了基本概念。

【讨论】:

    【解决方案2】:

    在问题中,您混合了两个部分 1. 身份验证和 2. 授权。

    1. 对于身份验证:可以制定表单身份验证。
    2. 对于授权:您必须在应用程序的 MVC 部分中实现自定义授权过滤器。参考:http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.118).aspx 它还可以用作 ASP.NET 表单的自定义属性。

    【讨论】:

      【解决方案3】:

      我找到了我认为正确的解决方案。在我的 web.config 中,我将 loginUrl 设置为我的 MVC 页面:

          <authentication mode="Forms">
            <forms name=".MyWebSite" enableCrossAppRedirects="true" loginUrl="Login" timeout="60" path="/" defaultUrl="~/Pages/Landing.aspx"></forms>
          </authentication>
      

      然后我必须在我的 authController 中设置 cookie,这样当我重定向到一个 aspx 页面时,HttpContext.CurrentUser 被定义为登录用户:

      FormsAuthentication.SetAuthCookie(model.Username, true);
      

      我不知道这是否确实是解决此问题的正确方法,但到目前为止它似乎正在工作。如果有人有任何反馈,我会保持打开状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-07
        • 2017-03-02
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 2018-08-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多