Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. using System;
  2. using System.Web.Http;
  3. using System.Web.Http.SelfHost;
  4.  
  5. namespace SelfHost
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var config = new HttpSelfHostConfiguration("http://myComputerName:8080");
  12. config.UseWindowsAuthentication = true;
  13.  
  14. config.Routes.MapHttpRoute(
  15. "API Default", "api/{controller}/{id}",
  16. new { id = RouteParameter.Optional });
  17.  
  18. using (HttpSelfHostServer server = new HttpSelfHostServer(config))
  19. {
  20. server.OpenAsync().Wait();
  21.  
  22. Console.WriteLine("Press Enter to quit.");
  23. Console.ReadLine();
  24. }
  25. }
  26. }
  27. }
  28.  
  29. [Authorize]
  30. public class HelloController : ApiController
  31. {
  32. public string Get()
  33. {
  34. // This next line throws an null reference exception if the Authorize
  35. // attribute is commented out.
  36. string userName = Request.GetUserPrincipal().Identity.Name;
  37. return "Hello " + userName;
  38. }
  39. }
  40.  
  41. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  42. <HTML><HEAD>
  43. <META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
  44. <BODY></BODY></HTML>
  45.  
  46. public class NtlmSelfHostConfiguration : HttpSelfHostConfiguration
  47. {
  48. public NtlmSelfHostConfiguration(string baseAddress)
  49. : base(baseAddress)
  50. { }
  51.  
  52. public NtlmSelfHostConfiguration(Uri baseAddress)
  53. : base(baseAddress)
  54. { }
  55.  
  56. protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
  57. {
  58. httpBinding.Security.Mode = HttpBindingSecurityMode.TransportCredentialOnly;
  59. httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
  60. return base.OnConfigureBinding(httpBinding);
  61. }
  62. }
  63.  
  64. var config = new NtlmSelfHostConfiguration("http://myComputerName:8080");
  65.  
  66. HttpSelfHostConfiguration.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
  67.  
  68. var handler = new HttpClientHandler();
  69. handler.UseDefaultCredentials = true;
  70. _httpClient = new HttpClient(handler);
  71.  
  72. using System.Web.Http;
  73. public class MyAuth : AuthorizeAttribute
  74. {
  75. public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
  76. {
  77. base.OnAuthorization(actionContext); //put breakpoint here
  78. }
  79. }
  80.  
  81. [Authorize]
  82. public class HelloController : ApiController
  83.  
  84. var handler = new HttpClientHandler();
  85. handler.UseDefaultCredentials = true;
  86. _httpClient = new HttpClient(handler);
  87.  
  88. public class NtlmSelfHostConfiguration : HttpSelfHostConfiguration
  89. {
  90. public NtlmSelfHostConfiguration(string baseAddress)
  91. : base(baseAddress)
  92. { }
  93.  
  94. public NtlmSelfHostConfiguration(Uri baseAddress)
  95. : base(baseAddress)
  96. { }
  97.  
  98. protected override BindingParameterCollection OnConfigureBinding(
  99. HttpBinding httpBinding)
  100. {
  101. if (this.BaseAddress.Scheme == Uri.UriSchemeHttps)
  102. {
  103. var ret = base.OnConfigureBinding(httpBinding);
  104. httpBinding.Security.Transport.ClientCredentialType =
  105. HttpClientCredentialType.Ntlm;
  106. return ret;
  107. }
  108.  
  109. httpBinding.Security.Mode = HttpBindingSecurityMode.TransportCredentialOnly;
  110. httpBinding.Security.Transport.ClientCredentialType =
  111. HttpClientCredentialType.Ntlm;
  112. return base.OnConfigureBinding(httpBinding);
  113. }
  114. }
  115.  
  116. var config = new NtlmSelfHostConfiguration("http://myComputerName:8080");
  117.  
  118. var config = new NtlmSelfHostConfiguration("https://myComputerName:8443");
  119.  
  120. public class Startup
  121. {
  122. public void Configuration(IAppBuilder app)
  123. {
  124. HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];
  125. listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
  126. }
  127. }
  128.  
  129. var config = new HttpSelfHostConfiguration("http://localhost:8080");
  130. config.UserNamePasswordValidator = new PHVValidator();
  131. config.Routes.MapHttpRoute(
  132. "API Default", "{controller}/{id}",
  133. new { id = RouteParameter.Optional });
  134.  
  135. using (HttpSelfHostServer server = new HttpSelfHostServer(config))
  136. {
  137. server.OpenAsync().Wait();
  138. Application.EnableVisualStyles();
  139. Application.SetCompatibleTextRenderingDefault(false);
  140. Application.Run(new DominusForm());
  141. }
  142.  
  143. public class PHVValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
  144. {
  145. public override void Validate(string userName, string password)
  146. {
  147. if (userName == "admin" && password == "123")
  148. {
  149. string[] rolarray = new string[] { "admin" };
  150. IPrincipal principal = new GenericPrincipal(new GenericIdentity(userName), rolarray);
  151. Thread.CurrentPrincipal = principal;
  152. }
  153. }
  154. }
  155.  
  156. [Authorize(Roles = "admin")]
  157. public HttpResponseMessage Get()
  158. {
  159. do things
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement