PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/TechEdWebApiDemo/App_Start/SecurityConfig.cs

https://github.com/garchibald/TechEdWebApiDemo
C# | 99 lines | 34 code | 21 blank | 44 comment | 1 complexity | 264393f37c4fd1d4974c4265c7b154db MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace TechEdWebApiDemo.App_Start
  6. {
  7. using System.Web.Http;
  8. using Resources.Security;
  9. using Thinktecture.IdentityModel.Tokens.Http;
  10. public class SecurityConfig
  11. {
  12. public static void ConfigureGlobal(HttpConfiguration globalConfig)
  13. {
  14. globalConfig.MessageHandlers.Add(
  15. new AuthenticationHandler(CreateConfiguration()));
  16. globalConfig.Filters.Add(new SecurityExceptionFilter());
  17. }
  18. public static AuthenticationConfiguration CreateConfiguration()
  19. {
  20. var config = new AuthenticationConfiguration
  21. {
  22. DefaultAuthenticationScheme = "Basic",
  23. EnableSessionToken = true
  24. };
  25. config.SessionToken.EndpointAddress = "/api/token";
  26. #region Basic Authentication
  27. config.AddBasicAuthentication((userName, password) => userName == password);
  28. #endregion
  29. #region Other Methods
  30. //#region SimpleWebToken
  31. //config.AddSimpleWebToken(
  32. // "http://identity.thinktecture.com/trust",
  33. // Constants.Realm,
  34. // Constants.IdSrvSymmetricSigningKey,
  35. // AuthenticationOptions.ForAuthorizationHeader("IdSrv"));
  36. //#endregion
  37. //#region JsonWebToken
  38. //config.AddJsonWebToken(
  39. // "http://selfissued.test",
  40. // Constants.Realm,
  41. // Constants.IdSrvSymmetricSigningKey,
  42. // AuthenticationOptions.ForAuthorizationHeader("JWT"));
  43. //#endregion
  44. //#region IdentityServer SAML
  45. //var idsrvRegistry = new ConfigurationBasedIssuerNameRegistry();
  46. //idsrvRegistry.AddTrustedIssuer("A1EED7897E55388FCE60FEF1A1EED81FF1CBAEC6", "Thinktecture IdSrv");
  47. //var idsrvConfig = new SecurityTokenHandlerConfiguration();
  48. //idsrvConfig.AudienceRestriction.AllowedAudienceUris.Add(new Uri(Constants.Realm));
  49. //idsrvConfig.IssuerNameRegistry = idsrvRegistry;
  50. //idsrvConfig.CertificateValidator = X509CertificateValidator.None;
  51. //config.AddSaml2(idsrvConfig, AuthenticationOptions.ForAuthorizationHeader("IdSrvSaml"));
  52. //#endregion
  53. //#region ACS SWT
  54. //config.AddSimpleWebToken(
  55. // "https://" + Constants.ACS + "/",
  56. // Constants.Realm,
  57. // Constants.AcsSymmetricSigningKey,
  58. // AuthenticationOptions.ForAuthorizationHeader("ACS"));
  59. //#endregion
  60. //#region AccessKey
  61. //var handler = new SimpleSecurityTokenHandler("my access key", token =>
  62. // {
  63. // if (ObfuscatingComparer.IsEqual(token, "accesskey123"))
  64. // {
  65. // return new ClaimsIdentity(new Claim[]
  66. // {
  67. // new Claim("customerid", "123")
  68. // }, "Custom");
  69. // }
  70. // return null;
  71. // });
  72. //config.AddAccessKey(handler, AuthenticationOptions.ForQueryString("key"));
  73. //#endregion
  74. #endregion
  75. return config;
  76. }
  77. }
  78. }