15

I am trying to play about with the asp.net MVC SPA template in visual studio 2013, I don't need any of the authentication bits, I just need to load directly onto one of the controllers pages.

How do I get rid of all the authentication stuff from the initial template?

4
  • have you considered <authentication mode="None" /> in web.config? here is more: msdn.microsoft.com/en-us/library/aa291347(v=vs.71).aspx Commented Jan 29, 2015 at 17:19
  • How do I set the entry point to the view I want? Commented Feb 1, 2015 at 0:16
  • Try this stackoverflow.com/questions/1333002/…, or you could look at maproute (If am not mistaken, been a while since I last used VC/MVC) Commented Feb 1, 2015 at 9:41
  • Slowly getting there, it no longer wants to go to the login page, and my default home page pops up for a second but then it tries to find "account/authorize" (which I had deleted because I don't want this built in authorization) I cannot find a reference to that controller in the existing code :/ Commented Feb 2, 2015 at 10:07

5 Answers 5

24

Remove the [Authorize] annotation from HomeController and remove this:

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

from Views\Home\Index.cshtml because one of does js is causing the redirect to the login page even after removing the [Authorize] annotation from HomeController and probably you don't need it. If you need these scripts in your page, then you need to edit one of them.

Sign up to request clarification or add additional context in comments.

Comments

9

Here's what I did.

Remove [Authorize] attribute from the home controller.

Then in app.viewmodel.js you'll see this:

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];
});

This is the section that will redirect you to the login screen, so comment out or remove the if block. If you want you can also go into app.datamodel.js and remove or comment out self.getAccessToken.

In addition, in WebApiConfig.cs you will probably want to remove / comment out the following lines:

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

Comments

3

Here is how I solved it. I just removed the

Removed the [Authorize] annotation from HomeController.cs (got this from Castro Roy's answer). Even after this the app redirected to the login page.

To resolve the redirection remove the [Authorize] annotation from the AccountController.cs

However I have retained the authentication related code so that can be used in other pages.

Comments

2

Put [AllowAnonymous] at the beginning of the function you want to allow anonymous access to.

Comments

0

In addition to removing [Authorize] from the controllers, the file home.viewmodel.js is causing the redirect problem on the home page load. In App_Start/BundleConfig.cs, remove the line ~/Scripts/app/home.viewmodel.js from the bundles/app ScriptBundle.

1 Comment

why does it trigger authorization ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.