Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web.Http;
- using System.Web.Http.SelfHost;
- namespace SelfHost
- {
- class Program
- {
- static void Main(string[] args)
- {
- var config = new HttpSelfHostConfiguration("http://myComputerName:8080");
- config.UseWindowsAuthentication = true;
- config.Routes.MapHttpRoute(
- "API Default", "api/{controller}/{id}",
- new { id = RouteParameter.Optional });
- using (HttpSelfHostServer server = new HttpSelfHostServer(config))
- {
- server.OpenAsync().Wait();
- Console.WriteLine("Press Enter to quit.");
- Console.ReadLine();
- }
- }
- }
- }
- [Authorize]
- public class HelloController : ApiController
- {
- public string Get()
- {
- // This next line throws an null reference exception if the Authorize
- // attribute is commented out.
- string userName = Request.GetUserPrincipal().Identity.Name;
- return "Hello " + userName;
- }
- }
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML><HEAD>
- <META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
- <BODY></BODY></HTML>
- public class NtlmSelfHostConfiguration : HttpSelfHostConfiguration
- {
- public NtlmSelfHostConfiguration(string baseAddress)
- : base(baseAddress)
- { }
- public NtlmSelfHostConfiguration(Uri baseAddress)
- : base(baseAddress)
- { }
- protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
- {
- httpBinding.Security.Mode = HttpBindingSecurityMode.TransportCredentialOnly;
- httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
- return base.OnConfigureBinding(httpBinding);
- }
- }
- var config = new NtlmSelfHostConfiguration("http://myComputerName:8080");
- HttpSelfHostConfiguration.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
- var handler = new HttpClientHandler();
- handler.UseDefaultCredentials = true;
- _httpClient = new HttpClient(handler);
- using System.Web.Http;
- public class MyAuth : AuthorizeAttribute
- {
- public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
- {
- base.OnAuthorization(actionContext); //put breakpoint here
- }
- }
- [Authorize]
- public class HelloController : ApiController
- var handler = new HttpClientHandler();
- handler.UseDefaultCredentials = true;
- _httpClient = new HttpClient(handler);
- public class NtlmSelfHostConfiguration : HttpSelfHostConfiguration
- {
- public NtlmSelfHostConfiguration(string baseAddress)
- : base(baseAddress)
- { }
- public NtlmSelfHostConfiguration(Uri baseAddress)
- : base(baseAddress)
- { }
- protected override BindingParameterCollection OnConfigureBinding(
- HttpBinding httpBinding)
- {
- if (this.BaseAddress.Scheme == Uri.UriSchemeHttps)
- {
- var ret = base.OnConfigureBinding(httpBinding);
- httpBinding.Security.Transport.ClientCredentialType =
- HttpClientCredentialType.Ntlm;
- return ret;
- }
- httpBinding.Security.Mode = HttpBindingSecurityMode.TransportCredentialOnly;
- httpBinding.Security.Transport.ClientCredentialType =
- HttpClientCredentialType.Ntlm;
- return base.OnConfigureBinding(httpBinding);
- }
- }
- var config = new NtlmSelfHostConfiguration("http://myComputerName:8080");
- var config = new NtlmSelfHostConfiguration("https://myComputerName:8443");
- public class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];
- listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
- }
- }
- var config = new HttpSelfHostConfiguration("http://localhost:8080");
- config.UserNamePasswordValidator = new PHVValidator();
- config.Routes.MapHttpRoute(
- "API Default", "{controller}/{id}",
- new { id = RouteParameter.Optional });
- using (HttpSelfHostServer server = new HttpSelfHostServer(config))
- {
- server.OpenAsync().Wait();
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new DominusForm());
- }
- public class PHVValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
- {
- public override void Validate(string userName, string password)
- {
- if (userName == "admin" && password == "123")
- {
- string[] rolarray = new string[] { "admin" };
- IPrincipal principal = new GenericPrincipal(new GenericIdentity(userName), rolarray);
- Thread.CurrentPrincipal = principal;
- }
- }
- }
- [Authorize(Roles = "admin")]
- public HttpResponseMessage Get()
- {
- do things
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement