Guest User

Untitled

a guest
Jun 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. public class Startup
  2. {
  3. public Startup(IConfiguration configuration, IHostingEnvironment env)
  4. {
  5. Configuration = configuration;
  6. hostingEnvironment = env;
  7. }
  8.  
  9. public IConfiguration Configuration { get; }
  10. private IHostingEnvironment hostingEnvironment { get; }
  11.  
  12. public void ConfigureServices(IServiceCollection services)
  13. {
  14. services.AddDbContextPool<HospitalContext>(options =>
  15. {
  16. options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
  17. options.UseOpenIddict();
  18. });
  19.  
  20. services.AddCors(options => options.AddPolicy("AllowLocalhost4200", builder =>
  21. {
  22. builder
  23. .WithOrigins("http://localhost:4200")
  24. .WithHeaders("Authorization", "Content-type")
  25. .WithMethods("Get", "Post", "Put", "Delete");
  26. }));
  27.  
  28. services.AddCustomIdentity();
  29. services.AddCustomOpenIddict(hostingEnvironment);
  30.  
  31. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  32. }
  33.  
  34. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  35. {
  36. if (env.IsDevelopment())
  37. {
  38. app.UseDeveloperExceptionPage();
  39. }
  40. else
  41. {
  42. app.UseHsts();
  43. }
  44.  
  45. app.UseCors("AllowLocalhost4200");
  46. app.UseAuthentication();
  47. app.UseDefaultFiles();
  48. app.UseStaticFiles();
  49. app.UseMvc();
  50. app.InitilizeDb();
  51. }
  52. }
  53.  
  54. public static IServiceCollection AddCustomOpenIddict(this IServiceCollection services,
  55. IHostingEnvironment env)
  56. {
  57. services.AddOpenIddict(options =>
  58. {
  59. options.AddEntityFrameworkCoreStores<HospitalContext>();
  60. options.AddMvcBinders();
  61. options.EnableTokenEndpoint("/connect/token");
  62. options.EnableAuthorizationEndpoint("/connect/authorize");
  63. options.AllowPasswordFlow()
  64. .AllowRefreshTokenFlow()
  65. .AllowImplicitFlow();
  66.  
  67. options.SetAccessTokenLifetime(TimeSpan.FromMinutes(30));
  68. options.SetIdentityTokenLifetime(TimeSpan.FromMinutes(30));
  69. options.SetRefreshTokenLifetime(TimeSpan.FromMinutes(60));
  70.  
  71. if (env.IsDevelopment())
  72. {
  73. options.DisableHttpsRequirement();
  74. }
  75.  
  76. options.AddEphemeralSigningKey();
  77. });
  78.  
  79. services.AddAuthentication(options =>
  80. {
  81. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  82. options.DefaultForbidScheme = JwtBearerDefaults.AuthenticationScheme;
  83. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  84. })
  85. .AddOAuthValidation();
  86.  
  87. return services;
  88. }
  89.  
  90. public authorize(model: ILoginModel): Observable<Response> {
  91. return this.http.post(`http://localhost:58300/connect/token`,
  92. this.authService.authFormBody(model),
  93. {headers: this.authService.authHeaders()});
  94. }
  95.  
  96. authHeaders(): Headers {
  97. const headers = new Headers(
  98. {
  99. 'Content-Type': 'application/x-www-form-urlencoded'
  100. });
  101. return headers;
  102. }
  103.  
  104. authFormBody(model: ILoginModel): string {
  105. let body = '';
  106. body += 'grant_type=password$';
  107. body += 'username=' + model.email + '&';
  108. body += 'password=' + model.password + '&';
  109. body += 'scope=OpenId profile OfflineAccess Roles';
  110. return body;
  111. }
Add Comment
Please, Sign In to add comment