Guest User

Untitled

a guest
Jul 12th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. [WebInvoke(UriTemplate = "widgets", Method = "POST")]
  2. public XElement CreateWidget(XElement e)
  3. {
  4. ...
  5. }
  6.  
  7. <?xml version="1.0"?>
  8. <configuration>
  9.  
  10. <connectionStrings>
  11. <add name="DatabaseConnectionString" connectionString="Data Source=.SQLEXPRESS;Initial Catalog=Database;Integrated Security=True" providerName="System.Data.SqlClient" />
  12. </connectionStrings>
  13. <system.web>
  14. <compilation debug="true" targetFramework="4.0" />
  15. <httpRuntime maxRequestLength="10485760" />
  16. </system.web>
  17.  
  18. <system.webServer>
  19. <modules runAllManagedModulesForAllRequests="true">
  20. <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  21. </modules>
  22. </system.webServer>
  23.  
  24. <system.serviceModel>
  25. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  26. <standardEndpoints>
  27. <webHttpEndpoint>
  28. <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="1048576" maxBufferSize="1048576" />
  29. </webHttpEndpoint>
  30. </standardEndpoints>
  31. </system.serviceModel>
  32.  
  33. </configuration>
  34.  
  35. <system.web>
  36. <compilation debug="true" targetFramework="4.0" />
  37. <httpRuntime maxRequestLength="10485760" />
  38. <authentication mode="None"></authentication>
  39. <httpModules>
  40. <add name="BasicAuthenticationModule" type="YourNamespace.UserNameAuthenticator" />
  41. </httpModules>
  42. </system.web>
  43.  
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Text;
  47. using System.Web;
  48. using System.Web.Security;
  49. using System.Security.Principal;
  50. using System.ServiceModel.Activation;
  51.  
  52. namespace YourNamespace
  53. {
  54. public class UserNameAuthenticator : IHttpModule
  55. {
  56. public void Dispose()
  57. {
  58. }
  59.  
  60. public void Init(HttpApplication application)
  61. {
  62. application.AuthenticateRequest += new EventHandler(this.OnAuthenticateRequest);
  63. application.AuthorizeRequest += new EventHandler(this.OnAuthorizationRequest);
  64. application.EndRequest += new EventHandler(this.OnEndRequest);
  65. }
  66.  
  67. public bool CustomAuth(string username, string password)
  68. {
  69. //TODO: Implement your custom auth logic here
  70. return true;
  71. }
  72.  
  73. public string[] GetCustomRoles(string username)
  74. {
  75. return new string[] { "read", "write" };
  76. }
  77.  
  78. public void OnAuthorizationRequest(object source, EventArgs eventArgs)
  79. {
  80. HttpApplication app = (HttpApplication)source;
  81. //If you want to handle authorization differently from authentication
  82. }
  83.  
  84. public void OnAuthenticateRequest(object source, EventArgs eventArgs)
  85. {
  86. HttpApplication app = (HttpApplication)source;
  87. //the Authorization header is checked if present
  88. string authHeader = app.Request.Headers["Authorization"];
  89. if (!string.IsNullOrEmpty(authHeader))
  90. {
  91. string authStr = app.Request.Headers["Authorization"];
  92. if (authStr == null || authStr.Length == 0)
  93. {
  94. // No credentials; anonymous request
  95. return;
  96. }
  97. authStr = authStr.Trim();
  98. if (authStr.IndexOf("Basic", 0) != 0)
  99. {
  100. //header not correct we do not authenticate
  101. return;
  102. }
  103.  
  104. authStr = authStr.Trim();
  105. string encodedCredentials = authStr.Substring(6);
  106. byte[] decodedBytes = Convert.FromBase64String(encodedCredentials);
  107. string s = new ASCIIEncoding().GetString(decodedBytes);
  108. string[] userPass = s.Split(new char[] { ':' });
  109. string username = userPass[0];
  110. string password = userPass[1];
  111. //the user is validated against the SqlMemberShipProvider
  112. //If it is validated then the roles are retrieved from the
  113. //role provider and a generic principal is created
  114. //the generic principal is assigned to the user context
  115. // of the application
  116. if (CustomAuth(username, password))
  117. {
  118. string[] roles = GetCustomRoles(username);
  119. app.Context.User = new GenericPrincipal(new
  120. GenericIdentity(username, "Membership Provider"), roles);
  121. }
  122. else
  123. {
  124. DenyAccess(app);
  125. return;
  126. }
  127. }
  128. else
  129. {
  130. //the authorization header is not present
  131. //the status of response is set to 401 and it ended
  132. //the end request will check if it is 401 and add
  133. //the authentication header so the client knows
  134. //it needs to send credentials to authenticate
  135. app.Response.StatusCode = 401;
  136. app.Response.End();
  137. }
  138. }
  139.  
  140. public void OnEndRequest(object source, EventArgs eventArgs)
  141. {
  142. if (HttpContext.Current.Response.StatusCode == 401)
  143. {
  144. //if the status is 401 the WWW-Authenticated is added to
  145. //the response so client knows it needs to send credentials
  146. HttpContext context = HttpContext.Current;
  147. context.Response.StatusCode = 401;
  148. context.Response.AddHeader("WWW-Authenticate", "Basic Realm");
  149. }
  150. }
  151. private void DenyAccess(HttpApplication app)
  152. {
  153. app.Response.StatusCode = 401;
  154. app.Response.StatusDescription = "Access Denied";
  155. // error not authenticated
  156. app.Response.Write("401 Access Denied");
  157. app.CompleteRequest();
  158. }
  159. } // End Class
  160. } //End Namespace
Add Comment
Please, Sign In to add comment