Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. var apiUrl = "http://andrewgodfroyportfolioapi.azurewebsites.net/api/Authentication";
  2. //alert(username + "|" + password + "|" + apiUrl);
  3. $.ajax({
  4. url: apiUrl,
  5. type: "POST",
  6. data: {
  7. username: username,
  8. password: password
  9. },
  10. contentType: "application/json; charset=utf-8",
  11. dataType: "json",
  12. success: function (response) {
  13. var authenticatedUser = JSON.parse(response);
  14. //alert("Data Loaded: " + authenticatedUser);
  15. if (onComplete != null) {
  16. onComplete(authenticatedUser);
  17. }
  18. },
  19. error: function (xhr, status, error) {
  20. //alert(xhr.responseText);
  21. if (onComplete != null) {
  22. onComplete(xhr.responseText);
  23. }
  24. }
  25. });
  26.  
  27. // This method gets called by the runtime. Use this method to add services to the container.
  28. public void ConfigureServices(IServiceCollection services)
  29. {
  30. // Add Cors
  31. services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
  32. {
  33. builder.AllowAnyOrigin()
  34. .AllowAnyMethod()
  35. .AllowAnyHeader();
  36. }));
  37.  
  38. // Add framework services.
  39. services.AddMvc();
  40. services.Configure<MvcOptions>(options =>
  41. {
  42. options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
  43. });
  44.  
  45. ...
  46. ...
  47. ...
  48. }
  49.  
  50. // This method gets called by the runtime. Use this method to configure
  51. //the HTTP request pipeline.
  52. public void Configure(IApplicationBuilder app, IHostingEnvironment env,
  53. ILoggerFactory loggerFactory)
  54. {
  55. loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  56. loggerFactory.AddDebug();
  57.  
  58. // Enable Cors
  59. app.UseCors("MyPolicy");
  60.  
  61. //app.UseMvcWithDefaultRoute();
  62. app.UseMvc();
  63.  
  64. ...
  65. ...
  66. ...
  67. }
  68.  
  69. [Route("api/[controller]")]
  70. [EnableCors("MyPolicy")]
  71. public class AdminController : Controller
  72.  
  73. .AddCustomHeader("Access-Control-Allow-Origin", "*")
  74. .AddCustomHeader("Access-Control-Allow-Methods", "*")
  75. .AddCustomHeader("Access-Control-Allow-Headers", "*")
  76. .AddCustomHeader("Access-Control-Max-Age", "86400")
  77.  
  78. .AddCustomHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE")
  79. .AddCustomHeader("Access-Control-Allow-Headers", "content-type, accept, X-PINGOTHER")
  80. .AddCustomHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Host, User-Agent, Accept, Accept: application/json, application/json, Accept-Language, Accept-Encoding, Access-Control-Request-Method, Access-Control-Request-Headers, Origin, Connection, Content-Type, Content-Type: application/json, Authorization, Connection, Origin, Referer")
  81.  
  82. app.UseSecurityHeadersMiddleware(new SecurityHeadersBuilder()
  83. .AddDefaultSecurePolicy()
  84. .AddCustomHeader("Access-Control-Allow-Origin", "http://localhost:3000")
  85. .AddCustomHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, PATCH, DELETE")
  86. .AddCustomHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Content-Type, Authorization"));
  87.  
  88. Install-Package Microsoft.AspNetCore.Cors
  89.  
  90. public void ConfigureServices(IServiceCollection services)
  91. {
  92. services.AddCors();
  93. }
  94.  
  95. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  96. {
  97. app.UseMvc();
  98.  
  99. app.UseCors(
  100. options => options.WithOrigins("http://example.com").AllowAnyMethod()
  101. );
  102. }
  103.  
  104. var JSON = JSON || {};
  105.  
  106. // implement JSON.stringify serialization
  107. JSON.stringify = JSON.stringify || function (obj) {
  108.  
  109. var t = typeof (obj);
  110. if (t != "object" || obj === null) {
  111.  
  112. // simple data type
  113. if (t == "string") obj = '"' + obj + '"';
  114. return String(obj);
  115.  
  116. }
  117. else {
  118.  
  119. // recurse array or object
  120. var n, v, json = [], arr = (obj && obj.constructor == Array);
  121.  
  122. for (n in obj) {
  123. v = obj[n]; t = typeof (v);
  124.  
  125. if (t == "string") v = '"' + v + '"';
  126. else if (t == "object" && v !== null) v = JSON.stringify(v);
  127.  
  128. json.push((arr ? "" : '"' + n + '":') + String(v));
  129. }
  130.  
  131. return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
  132. }
  133. };
  134.  
  135. // implement JSON.parse de-serialization
  136. JSON.parse = JSON.parse || function (str) {
  137. if (str === "") str = '""';
  138. eval("var p=" + str + ";");
  139. return p;
  140. };
  141.  
  142. data: JSON.stringify({
  143. username: username,
  144. password: password
  145. }),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement