VS2013 ASP.NET MVC Separate Application Template Page Automatic Redirection

The default SPA template in ASP.NET in Visual Studio 2013 provides us with a default website template. However, this template does not allow you to see the page / Main page / Index and is automatically redirected to / Account / Login. Now I want to use this nice feature, but not on my home page.

I tried the following:

  • comments on Authorize attribute
  • adding AllowAnonymous, OverrideAuthentication, etc. in sequence.
//[Authorize] [AllowAnonymous] [OverrideAuthentication] public class HomeController : Controller { [OverrideAuthentication] [AllowAnonymous] public ActionResult Index() { return View(); } } 

But he still redirects me to

http://example.com-00-009838/Account/Login?ReturnUrl=%2FAccount%2FAuthorize%3Fclient_id%3Dweb%26response_type%3Dtoken%26state%3D

whenever i go to

http://example.com-00-009838/Home/Index

How can I prevent this?

+2
source share
2 answers

First remove the Authorize attribute from the main controller

 //[Authorize] public class HomeController : Controller 

Then in /Views/Home/Index.cshtml delete the SPA logic (I commented on it):

 @section SPAViews { @Html.Partial("_Home") } @section Scripts{ @* @Scripts.Render("~/bundles/knockout") @Scripts.Render("~/bundles/app") *@ } 

Basically, what you are doing is making the homepage the plain old MVC page, which I assume you want to do.

Then you can go to the new page where your SPA application will be placed (and it is required that the user is signed up).

+4
source

In your controller:

 //[Authorize] public class HomeController : Controller 

In your knockout model

 app.addViewModel({ name: "Home", bindingMemberName: "home", factory: HomeViewModel, // add this line only if you require authorization, remove or set to false in your home view model authorize: true }); 

And finally, in your application presentation model:

 self[options.bindingMemberName] = ko.computed(function () { if (options.authorize && !dataModel.getAccessToken()) { ... } return self.Views[options.name]; }); 
+3
source

Source: https://habr.com/ru/post/1203496/


All Articles