1

I've got a controller with 3 action methods on it, two of which are regular OData calls and a third which is a function. Using Azure AD and ADAL to secure the WebAPI.

CustomAuthAttribute (IsAppAuthorizing Simply checks a web.config entry)

public class OpsmApiAuthorizeAttribute : AuthorizeAttribute
{
    /// <summary>
    /// Returns whether or not the user has authenticated with ADFS and whehter ornot we are configured to do authorization
    /// </summary>
    /// <param name="actionContext"></param>
    /// <returns></returns>
    protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        if (HttpContext.Current.IsAppAuthorizing())
            return base.IsAuthorized(actionContext);

        return true;
    }
}

Startup.Auth.cs

    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                TokenValidationParameters = new TokenValidationParameters
            {
                ValidAudience = ConfigurationManager.AppSettings["ida:Audience"],
            },
                MetadataAddress = ConfigurationManager.AppSettings["ida:MetadataEndpoint"],
            });
    }

Controller (ByUser is the OData function that does not get proper IPrincple information, other two methods work fine)

[OpsmApiAuthorizeAttribute]
public class ProjectsController : BaseController
{
    /// <summary>
    /// Get a Project Detail for a given project id
    /// </summary>
    /// <returns>json</returns>
    [EnableQuery]
    public IQueryable<OPSM.DataAccess.Database.OpsM.PRJ> Get([FromODataUri] string key)
    {
       ...
    }
    /// <summary>
    /// Get all Projects 
    /// </summary>
    /// <returns>json</returns>
    [EnableQuery]
    public IQueryable<OPSM.DataAccess.Database.OpsM.PRJ> Get()
    {
        ...
    }

    [HttpGet]
    //[CacheOutput(ServerTimeSpan = 60 * 60)]
    public IHttpActionResult ByUser([FromODataUri]string userId)
    {
       ...
    }
}
dtmnash
  • 166
  • 1
  • 7

0 Answers0