1

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 ?

1 Answer 1

6

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.