Skip to content

Instantly share code, notes, and snippets.

@NathanGloyn
Created June 10, 2013 13:01
Show Gist options
  • Save NathanGloyn/5748532 to your computer and use it in GitHub Desktop.
Save NathanGloyn/5748532 to your computer and use it in GitHub Desktop.
Configuration of Thinktecture.Identity45 where attempting to set FormsAuth cookie and use that for authentication
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ConfigureGlobal(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
private void ConfigureGlobal(HttpConfiguration globalConfig)
{
WebApiConfig.Register(GlobalConfiguration.Configuration);
globalConfig.MessageHandlers.Add(new AuthenticationHandler(CreateConfiguration()));
}
private AuthenticationConfiguration CreateConfiguration()
{
var config = new AuthenticationConfiguration
{
RequireSsl = false,
SendWwwAuthenticateResponseHeaders = false,
InheritHostClientIdentity = true
};
config.AddBasicAuthentication(UserCredentials.Validate);
return config;
}
protected void Application_PostAuthenticateRequest()
{
if (ClaimsPrincipal.Current.Identity.IsAuthenticated)
{
var principal = new ClaimsTransformer().Authenticate(string.Empty, ClaimsPrincipal.Current);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = principal;
}
}
}
[AllowAnonymous]
public class LogonController:ApiController
{
public HttpResponseMessage Get()
{
FormsAuthentication.SetAuthCookie("bob", false);
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
[Authorize]
public class TestController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment