[Authorize(Roles = "Administrator")]
public class ManagerController : BaseController
{
public ActionResult Home()
{
Guid userId = new Guid();
if (currentUser != null)
userId = (Guid)currentUser.ProviderUserKey;
var currEcole = Service.getSingleByUser(userId);
return View(currEcole);
}
}
And I use the MVC default AccountController to log On
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
//if (MembershipService.ValidateUser(model.UserName, model.Password))
if (MembershipService.ValidateEmail(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
Session["logon"] = model.UserName;
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Le nom d'utilisateur ou mot de passe fourni est incorrect.");
}
}
// Si nous sommes arrivés là, quelque chose a échoué, réafficher le formulaire
return View(model);
}
If I try the URL http://localhost/manager/home , the application must redirect me to the logOn view
And must log on as Administrator to Access Home page OK ( http://localhost:2386/Account/LogOn?ReturnUrl=%2fManager%2fhome )
The log On view is displayed and I log On as Administrateur But after logged on I'm not redirected to home page
Why ?
The debugger executes the instruction return Redirect(returnUrl); and the value of
returnUrl is /Manager/home
I logged on but the logon view is always displayed
First remove the Roles property and leave only the [Authorize] attribute. If this works then check if the user you're loggin with in in the Administrator role.
Please click 'Mark as Answer' if my reply has assisted you
Try with a new MVC3 Internet Application and then compare with your own. Maybe you haven't set the Membership providers correctly or the database connection.
Please click 'Mark as Answer' if my reply has assisted you
Can you upload the project somewhere (SkyDrive for example) so we can have a look? Or try to reproduce the problem on a simple project starting from an empty application
Please click 'Mark as Answer' if my reply has assisted you
Goraleye
Member
295 Points
370 Posts
Redirect to returnUrl does not work
Aug 02, 2011 05:24 PM|LINK
He,
I've a controller like this
[Authorize(Roles = "Administrator")] public class ManagerController : BaseController { public ActionResult Home() { Guid userId = new Guid(); if (currentUser != null) userId = (Guid)currentUser.ProviderUserKey; var currEcole = Service.getSingleByUser(userId); return View(currEcole); } }[HttpPost] public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { //if (MembershipService.ValidateUser(model.UserName, model.Password)) if (MembershipService.ValidateEmail(model.UserName, model.Password)) { FormsService.SignIn(model.UserName, model.RememberMe); if (Url.IsLocalUrl(returnUrl)) { Session["logon"] = model.UserName; return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "Le nom d'utilisateur ou mot de passe fourni est incorrect."); } } // Si nous sommes arrivés là, quelque chose a échoué, réafficher le formulaire return View(model); }princeG
Star
9612 Points
1602 Posts
Re: Redirect to returnUrl does not work
Aug 02, 2011 05:29 PM|LINK
Check return url: http://stackoverflow.com/questions/778607/returnurl-in-asp-net-mvc
http://stackoverflow.com/questions/6007537/asp-net-authentication-if-returnurl-null-userdata-comes-empty
http://stackoverflow.com/questions/827905/asp-net-mvc-login-returnurl-always-null
http://stackoverflow.com/questions/6448939/need-help-with-returnurl-in-mvc-3-loginpage
http://www.asp.net/mvc/tutorials/preventing-open-redirection-attacks
Goraleye
Member
295 Points
370 Posts
Re: Redirect to returnUrl does not work
Aug 07, 2011 05:59 PM|LINK
This does not work
raduenuca
All-Star
24675 Points
4250 Posts
Re: Redirect to returnUrl does not work
Aug 08, 2011 04:37 AM|LINK
First remove the Roles property and leave only the [Authorize] attribute. If this works then check if the user you're loggin with in in the Administrator role.
Radu Enuca | Blog
Goraleye
Member
295 Points
370 Posts
Re: Redirect to returnUrl does not work
Aug 08, 2011 07:47 AM|LINK
He,
I've tried the [Authorize] attribute only but, visitor logs on but the login view still always displayed.
The redirect instruction is executed but any redirect is fired.
raduenuca
All-Star
24675 Points
4250 Posts
Re: Redirect to returnUrl does not work
Aug 08, 2011 07:49 AM|LINK
Try with a new MVC3 Internet Application and then compare with your own. Maybe you haven't set the Membership providers correctly or the database connection.
Radu Enuca | Blog
amitpatel.it
Star
7956 Points
1865 Posts
Re: Redirect to returnUrl does not work
Aug 08, 2011 07:52 AM|LINK
Implement below code on your basecontroller
protected override void OnActionExecuted(ActionExecutedContext filterContext) { if (!Request.IsAuthenticated ) { filterContext.Result = RedirectToAction("LogOn", "Account"); // your login related pages } }Let me know if you have any questions
MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
Goraleye
Member
295 Points
370 Posts
Re: Redirect to returnUrl does not work
Aug 08, 2011 08:08 PM|LINK
This does not work
The function OnActionExecuted executes many times and finally displays the page http://localhost:2386/Manager/Home with body
Internet Explorer cannot display the webpage
The function ActionResult LogOn(LogOnModel model, string returnUrl) is never executed.
I think I will try recreate the logOn Model
raduenuca
All-Star
24675 Points
4250 Posts
Re: Redirect to returnUrl does not work
Aug 08, 2011 08:32 PM|LINK
Can you upload the project somewhere (SkyDrive for example) so we can have a look? Or try to reproduce the problem on a simple project starting from an empty application
Radu Enuca | Blog
Goraleye
Member
295 Points
370 Posts
Re: Redirect to returnUrl does not work
Aug 09, 2011 07:04 PM|LINK
He,
Thank for all
Best regards