Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Authentication.JwtBearer;
  7. using Microsoft.AspNetCore.Builder;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.AspNetCore.HttpsPolicy;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.EntityFrameworkCore;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Extensions.Options;
  16. using Microsoft.IdentityModel.Tokens;
  17. using TimeRegister.Helpers;
  18. using TimeRegister.Services;
  19.  
  20. namespace TimeRegister
  21. {
  22. public class Startup
  23. {
  24. public Startup(IConfiguration configuration)
  25. {
  26. Configuration = configuration;
  27. }
  28.  
  29. public IConfiguration Configuration { get; }
  30.  
  31. // This method gets called by the runtime. Use this method to add services to the container.
  32. /* public void ConfigureServices(IServiceCollection services)
  33. {
  34. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  35. }*/
  36.  
  37. public void ConfigureServices(IServiceCollection services)
  38. {
  39. services.AddScoped<IUserService, UserService>();
  40. var connection = @"data source=localhost; initial Catalog = MyTimeRegister;persist security info=True; Integrated Security=SSPI;";
  41.  
  42. //services.AddDbContext<DataContext>(options => options.UseSqlServer(connection));
  43. services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  44. .AddJwtBearer(options =>
  45. {
  46. options.TokenValidationParameters = new TokenValidationParameters
  47. {
  48. ValidateIssuer = true,
  49. ValidateAudience = true,
  50. ValidateLifetime = true,
  51. ValidateIssuerSigningKey = true,
  52.  
  53. ValidIssuer = "http://localhost:44305",
  54. ValidAudience = "http://localhost:44305",
  55. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345"))
  56. };
  57. });
  58. services.AddCors(options =>
  59. {
  60. options.AddPolicy("EnableCORS", builder =>
  61. {
  62. builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials().Build();
  63. });
  64. });
  65. services.AddMvc();
  66. }
  67.  
  68. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  69. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  70. {
  71. if (env.IsDevelopment())
  72. {
  73. app.UseDeveloperExceptionPage();
  74. }
  75. else
  76. {
  77. app.UseHsts();
  78. }
  79.  
  80. app.UseHttpsRedirection();
  81. app.UseAuthentication();
  82. app.UseCors("EnableCORS");
  83. app.UseMvc();
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement