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

col*_*gem 14 c# authentication asp.net-mvc single-page-application

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

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

Cas*_*Roy 24

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

@section Scripts{
   @Scripts.Render("~/bundles/knockout")
   @Scripts.Render("~/bundles/app")
}
Run Code Online (Sandbox Code Playgroud)

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


Mat*_*and 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];
});
Run Code Online (Sandbox Code Playgroud)

这是将您重定向到登录屏幕的部分,因此注释掉或删除该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));
Run Code Online (Sandbox Code Playgroud)