【问题标题】:Remove authentication in ASP.net MVC single page application删除 ASP.net MVC 单页应用程序中的身份验证
【发布时间】:2015-01-29 17:04:19
【问题描述】:

我正在尝试使用 Visual Studio 2013 中的 asp.net MVC SPA 模板,我不需要任何身份验证位,我只需要直接加载到控制器页面之一。

如何摆脱初始模板中的所有身份验证内容?

【问题讨论】:

  • 你在web.config中考虑过吗?这里还有更多:msdn.microsoft.com/en-us/library/aa291347(v=vs.71).aspx
  • 如何将入口点设置为我想要的视图?
  • 试试这个stackoverflow.com/questions/1333002/…,或者你可以看看maproute(如果我没记错的话,我上次使用VC/MVC已经有一段时间了)
  • 慢慢到达那里,它不再想去登录页面,我的默认主页弹出一秒钟但随后它试图找到“帐户/授权”(我已经删除了,因为我不想要这个内置授权)我在现有代码中找不到对该控制器的引用:/

标签: c# asp.net-mvc authentication single-page-application


【解决方案1】:

HomeController 中删除[Authorize] 注释并将其删除:

@section Scripts{
   @Scripts.Render("~/bundles/knockout")
   @Scripts.Render("~/bundles/app")
}

来自Views\Home\Index.cshtml,因为即使在从HomeController 中删除了[Authorize] 注释之后,js 之一也会导致重定向到登录页面,并且可能您不需要它。如果您的页面中需要这些脚本,则需要编辑其中一个。

【讨论】:

    【解决方案2】:

    这就是我所做的。

    从家庭控制器中删除[Authorize] 属性。

    然后在app.viewmodel.js 你会看到这个:

    self[options.bindingMemberName] = ko.computed(function () {
        if (!dataModel.getAccessToken()) {
            // The following code looks for a fragment in the URL to get the access token which will be
            // used to call the protected Web API resource
            var fragment = common.getFragment();
    
            if (fragment.access_token) {
                // returning with access token, restore old hash, or at least hide token
                window.location.hash = fragment.state || '';
                dataModel.setAccessToken(fragment.access_token);
            } else {
                // no token - so bounce to Authorize endpoint in AccountController to sign in or register
                window.location = "/Account/Authorize?client_id=web&response_type=token&state=" + encodeURIComponent(window.location.hash);
            }
        }
    
        return self.Views[options.name];
    });
    

    这是将您重定向到登录屏幕的部分,因此请注释掉或删除 if 块。如果您愿意,您也可以进入app.datamodel.js 并删除或注释掉self.getAccessToken

    此外,在WebApiConfig.cs 中,您可能希望删除/注释掉以下行:

    // Web API configuration and services
    // Configure Web API to use only bearer token authentication.
    config.SuppressDefaultHostAuthentication();
    config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
    

    【讨论】:

      【解决方案3】:

      这是我解决它的方法。我刚刚删除了

      从 HomeController.cs 中删除了 [Authorize] 注释(从 Castro Roy 的回答中得到)。即使在此之后,应用程序仍会重定向到登录页面。

      要解决重定向,请从 AccountController.cs 中删除 [Authorize] 注释

      但是我保留了认证相关的代码,以便可以在其他页面中使用。

      【讨论】:

        【解决方案4】:

        [AllowAnonymous]放在您要允许匿名访问的函数的开头。

        【讨论】:

          【解决方案5】:

          除了从控制器中删除 [Authorize] 之外,文件 home.viewmodel.js 还会导致主页加载时出现重定向问题。在App_Start/BundleConfig.cs 中,从bundles/app ScriptBundle 中删除~/Scripts/app/home.viewmodel.js 行。

          【讨论】:

          • 为什么会触发授权?
          猜你喜欢
          • 2016-05-30
          • 2014-06-19
          • 1970-01-01
          • 2019-06-30
          • 2016-10-01
          • 2023-03-22
          • 2018-12-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多