How to Authorize different login Url in MVC3?
Hello everyone I have two login but how can I assign these login URLs with using authorize ?
Here is my webconfig :
<location path="Home"> <system.web> <authorization> <deny users="?"/> </authorization> </system.web> </location> <authentication mode="Forms"> <forms loginUrl="~/Home/Login" timeout="2880" /> </authentication>
So how can I create second loginurl ?
Answers
You cannot have 2 logon urls in Forms Authentication. If you need to achieve this functionality you could write custom [Authorize] attributes and then override the HandleUnauthorizedRequest method where you could redirect to the corresponding logon url.
For example:
public class MyAuthorizeAttribute : AuthorizeAttribute { private readonly string _controller; private readonly string _action; public MyAuthorizeAttribute(): this("account", "logon") { } public MyAuthorizeAttribute(string controller, string action) { _controller = controller; _action = action; } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { var values = new RouteValueDictionary(new { controller = _controller, action = _action, returnurl = filterContext.HttpContext.Request.Url.PathAndQuery }); filterContext.Result = new RedirectToRouteResult(values); } }
and then:
public class SomeController { [MyAuthorize] public ActionResult Foo() { return View(); } [MyAuthorize("account", "someotherlogonaction")] public ActionResult Bar() { return View(); } }