1

I am using MVC 5 for ASP.NET.

I am trying to create a custom authorize attribute. Users are authenticated to my application, using OpenId and if a session variable exists. I have added the [OpenIdAuthorize] to my controllers. When I view them after sign in, I get bounced back to /openid/index in my application. The session variable exists. I added breakpoints, but they are never reached.

public class OpenIdAuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
{

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext.Session["FriendlyEmail"] == null)
            return false;
        else
            return true;
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if(filterContext.HttpContext.Session["FriendlyEmail"] == null)
            filterContext.Result = new RedirectResult("/openid/index");
    }
}

Here is my controller:

[OpenIdAuthorize]
public class RuleSetController : Controller
7
  • Do you override AuthorizeCore function? Commented Jun 15, 2017 at 14:40
  • Please see my updated answer. The issue persists. Commented Jun 15, 2017 at 14:45
  • 1
    Does any breakpoints on AuthorizeCore function getting hit? Commented Jun 15, 2017 at 14:52
  • 1
    Is the normal AuthorizeAttribute still active? It might have been added as a global filter in FilterConfig? Commented Jun 15, 2017 at 14:52
  • When I add it to filter config, the code does run. As soon as the user accesses my app, it tries to redirect them. Can I add code to make authorize happen on certain pages? Commented Jun 15, 2017 at 15:31

1 Answer 1

1

Here is my solution:

  1. Do not enter anything in filter.config.
  2. In the web.config, enter this code: (see authentication mode > forms https://support.microsoft.com/en-us/help/301240/how-to-implement-forms-based-authentication-in-your-asp.net-application-by-using-c-.net)
  3. Adjust the AuthorizeAttribute to only override AuthorizeCore. Override no other methods.

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext.Session["FriendlyEmail"] == null)
            return false;
        else
            return true;
    }
    
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.