Customize Authorize Redirect - asp.net-core-mvc

I am using Beta 4 and when I use the [Authorize] attribute, it redirects to /account/login like i'd expect but that's no the name of my URL. I could customize this in web.config but I don't know where to configure it in ASP.NET 5. Any ideas?

Not sure if this will help but I did download VS 2015 RC.
I’ve created a new MVC 6 website project and launched (F5) it. While being unauthenticated, I tried reaching the ManageController which is decorated with the [Authorize] attribute.
Needless to say, I was redirected to the Account/login view but nowhere have I found where this is configured.
I did manage to add the following inside the ConfigureServices() method of the Startup.cs:
services.Configure<CookieAuthenticationOptions>(options =>
{
options.LoginPath = new PathString("/Gazou/Index");
});
Just above the:
services.AddMvc();
I’ve then created my new GazouController with a simple Index IActionResult().
Ran the application again, tried accessing the ManageController but this time, I was redirected to the Index method of my GazouController instead of the default behavior.
Hope this helps.
Vince

I think you didn't format it with code so I can't see it, but this is what I found:
services.Configure<CookieAuthenticationOptions>(opt =>
{
opt.LoginPath = PathString.FromUriComponent("/Auth/Login");
});

Related

Tutorial ASP.NET Core app not seen by localhost

Using MS Visual Studio 2022 on Windows 10 pro to build an ASP.NET Core MVC for the first time. When this tutorial (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-6.0&tabs=visual-studio) has me run it, Localhost comes up with some stuff relating to the app (its name, a welcome message). The tutorial says to then add "/MvcMovie/" to the URL in the address field. Then, localhost says "No webpage was found for the web address: https://localhost:44379/MvcMovie/".
I'm new at this. Not sure from the tutorial when/if to actually add something to localhost.
Any help will be appreciated.
If you are refering to this:
In the Configure your new project dialog, enter MvcMovie for Project
name. It's important to name the project MvcMovie. Capitalization
needs to match each namespace when code is copied.
The tutorial is saying that your Project's name should be MvcMovie when you create it. Other than that, the tutorial doesn't ask you to go to localhost/mvcMovie. To figure out the routing in an MVC check the next few parts about Controllers-Views-Models. There will be explained about the routing part and how you can access different parts of the URL.
The MvcMovie mentioned in the document does not require you to enter in the URl, it is just the name of the created project:
If you need to use MvcMovie as the routing address in the Url, you can do this:
Controller:
[ApiController]
public class TestController : Controller
{
[HttpGet]
[Route("MvcMovie")]
public IActionResult MvcMovie()
{
return View();
}
}
View demo:
<h1>This is MvcMovie Controller!</h1>
Test url:https://localhost:7040/MvcMovie
Result:
I think you need to know the routing rules in mvc, you can read this document:
Routing to controller actions in ASP.NET Core

How to redirect to action of another controller of an referenced project

Hi i have a created an another project inside my application.
Now i want to move to the action of the Controller in the new project from the action in my old project.
I tried many things like:
return RedirectToAction("ActivityPosting", new RouteValueDictionary(new { controller = "ActivityPosting", action = "ActivityPosting" }));
Then...
return RedirectToAction("ActivityPosting", "ActivityPosting")
But with this it moves to the action but does not show up the View.
I think the problem may be with the port number.
the older project has port number:http://localhost:18520
and the new project has port number:http://localhost:52146
But i dont know how to redirect to a new Port.
Please help me with this.
The RedirectToAction method is intended to be used to perform redirects only within the same project. If you want to redirect to another application you should use an absolute url and the Redirect method:
return Redirect("http://example.com/foo/bar");

Best way to deal with renaming a controller

Working with ASP.NET MVC3, site is in beta and customer decided to rename one of the controllers.
http://domain.com/foo[/*] -> http://domain.com/bar[/*]
What is the most straightforward way to handle redirecting so I don't break any foo bookmarks?
Keep the old controller around so the old URLs still work.
Or add a rewrite rule. Something like:
domain.com/foo(/[_0-9a-z-]+)
to:
domain.com/bar{R:1}
URL Rewrite in IIS
http://technet.microsoft.com/en-us/library/ee215194(WS.10).aspx
http://www.iis.net/download/URLRewrite
If you are using MVC.NET you probably already have URL Rewrite installed.
Another option would be to register a specific route for the old controller name in the Global.asax.cs.
routes.MapRoute(
"RenamedController", // Route name
"[OldControllerName]/{action}/{id}", // URL with parameters
new { controller = "[NewControllerName]", action = "Index", id = "" } // Parameter defaults
);
Add that before the standard default route, and your new controller should respond to both old and new names.
A 302 redirect would be fine, if you can figure out how to do that in IIS. This screenshot suggests it's not that arduous. Alternately, if you're using Castle Windsor you may want to register an interceptor that uses HttpResponse.Redirect()
REST standard suggests the best way to handle this issue is by returning a 301(Moved permanently request). Stack Overflow Post REST Standard
In .Net I recommend using Controller.RedirectToActionPermanent in your controller. See: ASP.NET, .NET Core
Code Example(should work for both ASP and Core):
public class MyController : ControllerBase
{
public IActionResult MyEndpoint(string routeValues1, string routeValues2)
{
return RedirectToActionPermanent("action", "controller", new { value1 = routeValues1, value2 = routeValues2 });
}
}
using MapRoute doesn't make sense in this case. MapRoute is really meant to provide a custom routing solution throughout the system. Its not really meant to deal with individual Redirects. As far as I'm aware it doesn't actually inform the user they are being redirected. See: Creating Custom Routes (C#)

ASP.NET MVC2 Empty Project Not Loading

My environment consists of Visual Studio 2010 and Windows 7 a few months ago I developed an MVC2 application with no problems however after trying to create a new project recently I received the error below.
I did find the link http://support.microsoft.com/kb/894670 but this is not relevant because I am not using IIS for testing, just F5 to get this working :)
Any ideas or help would be appreciated.
Server Error in '/' Application.
--------------------------------------------------------------------------------
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4952; ASP.NET Version:2.0.50727.4927
The Empty MVC 2 template application does not define any controllers or views. When you create a new Empty MVC 2 application and immediately run it, you will see the error message you posted.
If you check the Global.asax file, you'll see that the project template automatically registers a default route specifying a default controller named "Home" and a default action named "Index".
To get this to run, right-click on the Controllers folder, then choose Add->Controller... Name the controller "HomeController". You can leave the check box to "Add action methods for Create, Update, Delete..." unchecked.
In the HomeController.cs file, right click in the Index() method and choose Add View...
Leave the View name as "Index", uncheck "Select master page", and then click Add.
In the Index view, you can enter some HTML and run the project; you should now see the page rendered by Index.aspx.
One thing I am not sure of is why your error message lists the .NET Framework version as 2.0. If you still have problems, check the target framework on your project properties.
Make sure that the routes are set properly and that you have default values for a controller and action. For example if you have the following route:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
Make sure that you have a HomeController with an Index action.

Introducing MVC into ASP.Net - 404 error

I have an existing ASP.Net Application, into which I am attempting to introduce MVC2.
Hopefully I can remember the steps, but what I did was the following:
Created a dummy MVC2 project.
Compared and merged .csproj, resulting in the Add Item commands showing MVC2 items.
Compared and merged the web.config
Compared and merged the global.asax.cs
Added Models, Views and Controllers directories
Added HostController with Index action and Index.aspx (no logic)
Amended route to make /Host/Index the default
So now, when I access the application via the root address http://localhost/MyApp it all works.
But, when I access http://localhost/MyApp/Host/Index I get a 404 error. I get the same result for any of the Controller/Actions I created. The only way I can get them to appear is to use the defaults in the routing configuration. I installed Phill Haack's route debugger and it's doing nothing. Obviously there's some problem with my routing, but I can't figure it out.
Any ideas what I've missed?
Bah... turns out that this is to do with IIS 5.1 and MVC routing.
I resolved it by using the following Routes in my application (note the .aspx extensions).
routes.MapRoute("Root", "", new { controller = "Host", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}.aspx", new { controller = "Host", action = "Index" });
Means I can't have clean routes, but at least it works.

Resources