Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Net.Http.Headers;
  6. using System.Security.Principal;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Web;
  10. using System.Configuration;
  11.  
  12. namespace WebAPISample.Modules
  13. {
  14. public class BasicAuthHttpModule : IHttpModule
  15. {
  16. private const string Realm = "sample.local";
  17.  
  18. public void Init(HttpApplication context)
  19. {
  20. // Register event handlers
  21. context.AuthenticateRequest += OnApplicationAuthenticateRequest;
  22. context.EndRequest += OnApplicationEndRequest;
  23. }
  24.  
  25. private static void SetPrincipal(IPrincipal principal)
  26. {
  27. Thread.CurrentPrincipal = principal;
  28. if (HttpContext.Current != null)
  29. {
  30. HttpContext.Current.User = principal;
  31. }
  32. }
  33.  
  34. // TODO: Here is where you would validate the username and password.
  35. private static bool CheckPassword(string username, string password)
  36. {
  37. bool validUser = false;
  38.  
  39. // put your database or authentication calls here
  40.  
  41. validUser = username == "test" && password == "test";
  42.  
  43. return validUser;
  44. }
  45.  
  46. private static bool AuthenticateUser(string credentials)
  47. {
  48. bool validated = false;
  49. try
  50. {
  51. var encoding = Encoding.GetEncoding("iso-8859-1");
  52. credentials = encoding.GetString(Convert.FromBase64String(credentials));
  53.  
  54. int separator = credentials.IndexOf(':');
  55. string name = credentials.Substring(0, separator);
  56. string password = credentials.Substring(separator + 1);
  57.  
  58. validated = CheckPassword(name, password);
  59. if (validated)
  60. {
  61. var identity = new GenericIdentity(name);
  62. SetPrincipal(new GenericPrincipal(identity, null));
  63. }
  64. }
  65. catch (FormatException)
  66. {
  67. // Credentials were not formatted correctly.
  68. validated = false;
  69.  
  70. }
  71. return validated;
  72. }
  73.  
  74. private static void OnApplicationAuthenticateRequest(object sender, EventArgs e)
  75. {
  76. var request = HttpContext.Current.Request;
  77. var authHeader = request.Headers["Authorization"];
  78. if (authHeader != null)
  79. {
  80. var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);
  81.  
  82. // RFC 2617 sec 1.2, "scheme" name is case-insensitive
  83. if (authHeaderVal.Scheme.Equals("basic",
  84. StringComparison.OrdinalIgnoreCase) &&
  85. authHeaderVal.Parameter != null)
  86. {
  87. AuthenticateUser(authHeaderVal.Parameter);
  88. }
  89. }
  90. }
  91.  
  92. // If the request was unauthorized, add the WWW-Authenticate header
  93. // to the response.
  94. private static void OnApplicationEndRequest(object sender, EventArgs e)
  95. {
  96. var response = HttpContext.Current.Response;
  97.  
  98. // see if the request sent an X-Requested-With header (Non-Browser request -
  99. // used by jQuery and Angular implementations to prevent the browser from
  100. // presenting the default Login dialog)
  101. var request = HttpContext.Current.Request;
  102.  
  103. string authType = "Basic";
  104.  
  105. if (response.StatusCode == 401)
  106. {
  107. if (request.Headers.AllKeys.Contains("X-Requested-With"))
  108. {
  109. if (request.Headers["X-Requested-With"] == "XMLHttpRequest")
  110. {
  111. authType = "xBasic";
  112. }
  113. }
  114.  
  115. response.Headers.Add("WWW-Authenticate",
  116. string.Format("{0} realm="{1}"", authType, Realm));
  117. }
  118. }
  119.  
  120. public void Dispose()
  121. {
  122. }
  123. }
  124. }
  125.  
  126. <system.webServer>
  127. <modules>
  128. <add name="BasicAuthHttpModule" type="WebAPISample.Modules.BasicAuthHttpModule, WebAPISample"/>
  129. </modules>
  130. </system.webServer>
  131.  
  132. using System;
  133. using System.Collections.Generic;
  134. using System.Linq;
  135. using System.Text;
  136. using System.Data;
  137. using System.Net.Http;
  138. using System.Web.Http;
  139. //using System.Web.Http.Cors;
  140.  
  141. namespace AppWS
  142. {
  143. [Authorize]
  144. //[EnableCors(origins: "*", headers: "*", methods: "*")]
  145. public class myService:IServicio
  146. {
  147. public EPlaca ListarVehicles(string sUsuario)
  148. {
  149. oService = new mHost.ServicioClient("Binding_IServicio");
  150. var rpta = oService.ListarVehicles(sUsuario);
  151. oService.Close();
  152. return rpta;
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement