Replies: 5 comments
-
Notice that the FYI, your code is hard to read. Take a look here on how to format it. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer and sorry for the formatting. Yeah, but I just tried a few things, e.g. changing I came up with the idea to add the return url parameter to the formdata instead by adding a hidden element Not sure if there are any errors or flaws in this approach, from a security standpoint or something else, but to me it seems more robust to always post to the same actionurl? |
Beta Was this translation helpful? Give feedback.
-
Using a hidden form field can be a useful way to get information back to the server in a round trip if you are already using a form. You should also be able to use the query string though. I'm not entirely clear on what the problem is without a code sample. |
Beta Was this translation helpful? Give feedback.
-
http://localhost:1934/Admin routes.MapRoute(
|
Beta Was this translation helpful? Give feedback.
-
Use the Redirect method in your controller and pass the name of the route you have mapped along with any parameters required by the route. Consider using attribute routing with named routes, it makes things easier. There is very good documentation on the ASP.NET website. |
Beta Was this translation helpful? Give feedback.
-
So the problem I'm facing is with boilerplate and mvc 5. I have managed to get Identity working etc. but if I authorize an action that then returns me to the login page, this is just fine on get.
e.g. if I go to /admin/ without being logged in, I get redirected to
"/admin/account/login/?returnurl=%2fadmin%2f"
and this does display the login page just fine, but with
the actionurl becomes "/admin/account/login/?ReturnUrl=%2Fadmin%2F"
If I submit the form I keep returning to the login page, as if I'm redirected there, and no I'm not logged in either. Basically it cannot find the route it's posting to. The Get it can find though, which is strange?
As I said I can login normally if I'm at just "/admin/account/login/", because then the action is the same.
The problem seems to be that it cannot find the correct route. I'm using attribute routing and have followed the Boilerplate samples that were in there to start and then built upon that to add new constant routes and action values.
For the login it looks like this (with [RoutePrefix("admin/account")] ):
[HttpPost, ActionName(AccountControllerAction.Login)]
[AllowAnonymous]
[ValidateAntiForgeryToken]
[Route("login", Name = AccountControllerRoute.PostLogin)]
public async Task PostLogin(LoginViewModel model, string returnUrl)
I have tried various setups using [NoTrailingSlash]
and also in the RouteConfig.cs I tried things like this:
routes.MapRoute(
"Login",
"admin/account/postlogin/{anything}",
new { controller = "account", action = "postlogin" },
new { anything = @"^(?ReturnUrl=.)?$" }
);
but nothing has worked where I can keep trailing slashes.
The only thing that has "worked" is if I set
routes.AppendTrailingSlash = false;
and then also use [NoTrailingSlash] on the postlogin.
This does affect all the pages though and all my AJAX calls etc have get their trailing slashes stripped etc., which is really annoying.
Maybe I could solve this by using a form post using javascript/AJAX instead but I think it's weird that I cannot get this working. I assume it has to do with attribute routing but perhaps I'm just missing something as I implemented the Identity stuff myself into the Boilerplate template, but maybe you or someone else know how to solve this?
Beta Was this translation helpful? Give feedback.
All reactions