Skip to content

Instantly share code, notes, and snippets.

@jelster
Created March 22, 2012 18:31
Show Gist options
  • Save jelster/2161370 to your computer and use it in GitHub Desktop.
Save jelster/2161370 to your computer and use it in GitHub Desktop.
Azure ACS Authorize attribute MVC4Beta + WebAPI Beta
public class AuthenticateAndAuthorizeAcsMvcAttribute : System.Web.Mvc.AuthorizeAttribute
{
public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
{
var principal = filterContext.HttpContext.User;
if (principal == null || !principal.Identity.IsAuthenticated)
{
filterContext.Result = new ViewResult()
{
ViewName = "AcsLogin",
ViewData = filterContext.Controller.ViewData,
TempData = filterContext.Controller.TempData
};
return;
}
base.OnAuthorization(filterContext);
}
}
public class AuthenticateAndAuthorizeAcsApiAttribute : System.Web.Http.AuthorizeAttribute
{
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var principal = actionContext.Request.GetUserPrincipal();
if (principal == null || !principal.Identity.IsAuthenticated)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
return;
}
actionContext.Response = new HttpResponseMessage(HttpStatusCode.OK);
}
}
@jelster
Copy link
Author

jelster commented Mar 22, 2012

@jelster
Copy link
Author

jelster commented Mar 23, 2012

Updated the Gist to derive the API filter from System.Web.Http.AuthorizeAttribute instead of implementing IAuthorizationFilter. Let the base class do all the work for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment