在ASP.net MVC单页面应用程序中删除身份验证

时间:2015-01-29 17:04:20

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

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

如何从初始模板中删除所有身份验证资料?

5 个答案:

答案 0 :(得分:23)

[Authorize]删除HomeController注释并删除它:

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

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

答案 1 :(得分:9)

这就是我的所作所为。

从家庭控制器中删除[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));

答案 2 :(得分:2)

以下是我如何解决它。我刚刚删除了

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

要解决重定向,请从AccountController.cs

中删除[Authorize]注释

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

除了从控制器中删除 function PrintWindow() { var isIE11 = !!navigator.userAgent.match(/Trident.*rv[ :]*11\./); if (navigator.appName == "Microsoft Internet Explorer" || isIE11== true) { var PrintCommand = '<OBJECT ID="PrintCommandObject" WIDTH=0 HEIGHT=0 '; PrintCommand += 'CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; document.body.insertAdjacentHTML('beforeEnd', PrintCommand); PrintCommandObject.ExecWB(6, 2); PrintCommandObject.outerHTML = ""; window.close(); } else { window.print(); } } 之外,文件home.viewmodel.js还会导致主页加载时出现重定向问题。在[Authorize]中,从App_Start/BundleConfig.cs ScriptBundle中删除行~/Scripts/app/home.viewmodel.js

相关问题