5

In my c# app I am adding a custom attribute like this

public class CheckLogIn : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (!httpContext.Request.IsAuthenticated)
            return false;
        return (Auth.UserLoggedIn || Auth.AdminLoggedIn);
    }
}

I am calling this attribute like this

[CheckLogIn]
public dynamic Create(String projectName, String organizationId)
{
    Project pro = Project.Create(organizationId, projectName).Save();

    return new
    {
        organizationId = pro.OrganizationId,
        name = pro.Name,
        id = pro.Id
    };
}

The AuthorizeCore is not being called. I have a break point there but it seems like it never gets called. Am I missing something here?

I have tried calling [CheckLogIn] from a regular controller it works, from an api controller it doesn't.

2
  • Did you register it on application start? Commented Mar 19, 2015 at 13:34
  • No but I have tried calling [CheckLogIn] from a regular controller it works, from an api controller it doesn't. Commented Mar 19, 2015 at 13:35

1 Answer 1

19

The System.Web.Http.AuthorizeAttribute for WebApi is a different type than the System.Web.Mvc.AuthorizeAttribute for MVC.

The reason why it doesn't work with WebApi is because WebApi doesn't know about MVC types. You need to create a type that inherits System.Web.Http.AuthorizeAttribute for that purpose.

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.