0

This question is mostly a duplicate of this one, but that was asked and answered in 2013 and the accepted answer recommends using MADAM, a project which hasn't been updated since 2012 and which I'm therefore not keen to implement in my solution.

I have an ASP.NET Web Api 2 project which includes an endpoint that returns an iCalendar:

[Authorize(Roles = "Staff")]
[HttpGet]
[Route("ical/{staffUsername}")]
public HttpResponseMessage GetStaffICalendar(string staffUsername)
{
    string calendarString = ICalendarService.GetICalendarForStaffMember(staffUsername);

    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(calendarString, System.Text.Encoding.UTF8, "text/calendar");

    return response;
}

This works just fine without the Authorize attribute on the method - I'm able to open and subscribe to the calendar in Outlook 2016 - but I want users subscribing to the iCalendar to, at the very least, belong to the "Staff" role. Ideally it'd only work if the username passed in the URL matched the username of the authenticated user (i.e. you could only subscribe to your own iCalendar), but restricting authorization to people in the "Staff" role would be sufficient.

With the Authorization attribute decorating the method, if I attempt to add the iCalendar to Outlook I get prompted for my username and password (good) but whatever variation of my username I enter (username only, domain\username, username@domain) authentication always fails (bad). This happens even if I remove the role from the attribute - i.e. [Authorize] instead of [Authorize(Roles = "Staff")].

How can I implement authentication for my Web Api 2 endpoint that will work with Outlook?

Philip Stratford
  • 4,513
  • 4
  • 45
  • 71
  • It seems like the problem is probably going to be where you are authenticating. You are probably not getting any roles back from the auth request. Are you able to put in a break point on the auth method and see what is returned. Specifically the roles that are returned, if any? – Patrick Mcvay Jun 10 '20 at 16:02
  • I put a breakpoint in `OnAfterAuthenticateRequest` in `Global.asax.cs` but it never gets past the first line: `var identity = HttpContext.Current.User.Identity`. That's what's triggering Outlook's authentication popup, as you'd expect, but because the authentication always fails I don't get any further - certainly not as far as assigning roles! – Philip Stratford Jun 10 '20 at 16:22

0 Answers0