You could write a custom Authorize attribute which instead if redirecting will directly render the LogOn view.
Up vote 0 down vote favorite share g+ share fb share tw.
Earlier I had similar issue. I have created asp mvc default template project and set authorization attribute on home controller. When I run app url is: http://localhost:48403/Account/LogOn?
ReturnUrl=%2f what I try to get is just http://localhost:48403 when user is no authenticated but I just have no luck with reouting setup :( I have tried to put this in global. Asax but no luck: routes. MapRoute("Login", "", new { controller = "Account", action = "LogOn" } ); This is my whole global.
Asax routes. MapRoute("About", "About", new { controller = "Home", action = "About" } ); routes. MapRoute("Login", "", new { controller = "Account", action = "LogOn" } ); routes.
MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter. Optional } // Parameter defaults ); asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing link|improve this question asked Feb 11 at 21:261110597314 86% accept rate.
I have just created a new ASP.NET MVC 3 application using the default template, decorated my HomeController with the Authorize attribute, changed my routes in global. Asax to be exactly as in your question, run the application and the url was http://localhost:xxxx/ and the Logon action was shown. Obviously HomeController was never executed because you have put the route for the Account before the default route.
What exactly are you trying to achieve? – Darin Dimitrov Feb 12 at 8:30 If I remove "Login" route than my url is like 'localhost:xxxx/Account/LogOn? ReturnUrl=%2f';.
And when it's there url is fine 'localhost:xxxx/'; but after I login I can't get to HomeController. Should I change default url or something, I don't know anymore – 1110 Feb 12 at 10:01 what is your goal? Please describe what you need in terms of interaction with your site.
If you want to get to the Index action of HomeController you will have to use /Home/Index instead of / because / will always render the Logon action according to your routes. – Darin Dimitrov Feb 12 at 10:07 Yes because site requires from user to be authenticated. After user is logged in I need to route him to Home/Index and if user comes to site and he is already authenticated I should route him directly to Home/Index.
LogOff must get back to LogOn. – 1110 Feb 12 at 10:09 What if an anonymous user tries for example to directly access /SomeController/SomeAction (by typing it in the address bar) and this action is protected? He should normally see the LogOn screen and which url do you need to see in the address bar in this case?
Upon successful login to which controller action should he be redirected: /Home/Index or SomeController/SomeAction? – Darin Dimitrov Feb 12 at 10:11.
You could write a custom Authorize attribute which instead if redirecting will directly render the LogOn view: public class MyAuthorizeAttribute : AuthorizeAttribute { protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { var viewResult = new ViewResult { ViewName = "~/Views/Account/LogOn. Cshtml" }; filterContext. Result = viewResult; } } then decorate your HomeController with it: MyAuthorize public class HomeController : Controller { public ActionResult Index() { return View(); } } then your AccountController does no longer need to worry about ReturnUrls: public class AccountController : Controller { HttpPost public ActionResult LogOn(LogOnModel model) { if (ModelState.
IsValid) { if (Membership. ValidateUser(model. UserName, model.
Password)) { FormsAuthentication. SetAuthCookie(model. UserName, model.
RememberMe); // TODO: obviously here instead of hardcoding the country and the city // you might want to retrieve them from your backend given the username return RedirectToAction("Index", "Home", new { country = "uk", city = "london" }); } else { ModelState. AddModelError("", "The user name or password provided is incorrect. "); } } return View(model); } public ActionResult LogOff() { FormsAuthentication.SignOut(); return RedirectToAction("Index", "Home"); } } and finally you will need to modify the action of the form in ~/Views/Account/LogOn.
Cshtml to point to the correct controller: @using (Html. BeginForm("LogOn", "Account")) { ... } You could leave your routes by default: public static void RegisterRoutes(RouteCollection routes) { routes. IgnoreRoute("{resource}.
Axd/{*pathInfo}"); routes. MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter. Optional } ); }.
Thanks. One more thing I will figure out myself. When user is not logged in and click on about and then login user still goes to Home/Index but should go to About – 1110 Feb 12 at 10:55 @1110, that's exactly what I asked you in my previous comments to your question and you said that no matter which url the user typed in his address bar, upon successful authentication he should be redirected to Home/Index.
Obviously since you didn't want to have the ReturnUrl query string parameter in your address bar when logging in there's no way for the LogOn controller action to know to which url it must redirect upon successful login. The purpose of this parameter is exactly that. – Darin Dimitrov Feb 12 at 17:06.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.