Created
June 10, 2013 13:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AllowAnonymous] | |
public class LogonController:ApiController | |
{ | |
public HttpResponseMessage Get() | |
{ | |
FormsAuthentication.SetAuthCookie("bob", false); | |
return new HttpResponseMessage(HttpStatusCode.OK); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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