Wednesday, July 20, 2016

Automatically redirect to public view

Leave a Comment

My ASP.NET Core app requires users to login. I created a public action method that allows anonymous users i.e.

[AllowAnonymous] public IActionResult Public() {    return View(); } 

Because in my Startup.cs I require authentication, users are automatically redirected to the login page.

enter image description here

I want to change the behavior and redirect users to the public page automatically and let them click a link to login.

How do I redirect my users to the public page instead of the login page?

1 Answers

Answers 1

Just change default mapping in App_Start:

routes.MapRoute(         "Default", // Route name         "{controller}/{action}/{id}", // URL with parameters*         new { controller = "Public", action = "Public",          id = UrlParameter.Optional } ); 

UPDATE

After your comment I think this will answer your needs:

services.Configure<IdentityOptions>(options => {     options.Cookies.ApplicationCookie.LoginPath = new PathString("/Home/Public"); }); 

Add it to the ConfigureServices method in Startup.cs

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment