Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Startup
- {
- public Startup(IConfiguration configuration, IHostingEnvironment env)
- {
- Configuration = configuration;
- hostingEnvironment = env;
- }
- public IConfiguration Configuration { get; }
- private IHostingEnvironment hostingEnvironment { get; }
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddDbContextPool<HospitalContext>(options =>
- {
- options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
- options.UseOpenIddict();
- });
- services.AddCors(options => options.AddPolicy("AllowLocalhost4200", builder =>
- {
- builder
- .WithOrigins("http://localhost:4200")
- .WithHeaders("Authorization", "Content-type")
- .WithMethods("Get", "Post", "Put", "Delete");
- }));
- services.AddCustomIdentity();
- services.AddCustomOpenIddict(hostingEnvironment);
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseHsts();
- }
- app.UseCors("AllowLocalhost4200");
- app.UseAuthentication();
- app.UseDefaultFiles();
- app.UseStaticFiles();
- app.UseMvc();
- app.InitilizeDb();
- }
- }
- public static IServiceCollection AddCustomOpenIddict(this IServiceCollection services,
- IHostingEnvironment env)
- {
- services.AddOpenIddict(options =>
- {
- options.AddEntityFrameworkCoreStores<HospitalContext>();
- options.AddMvcBinders();
- options.EnableTokenEndpoint("/connect/token");
- options.EnableAuthorizationEndpoint("/connect/authorize");
- options.AllowPasswordFlow()
- .AllowRefreshTokenFlow()
- .AllowImplicitFlow();
- options.SetAccessTokenLifetime(TimeSpan.FromMinutes(30));
- options.SetIdentityTokenLifetime(TimeSpan.FromMinutes(30));
- options.SetRefreshTokenLifetime(TimeSpan.FromMinutes(60));
- if (env.IsDevelopment())
- {
- options.DisableHttpsRequirement();
- }
- options.AddEphemeralSigningKey();
- });
- services.AddAuthentication(options =>
- {
- options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- options.DefaultForbidScheme = JwtBearerDefaults.AuthenticationScheme;
- options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- })
- .AddOAuthValidation();
- return services;
- }
- public authorize(model: ILoginModel): Observable<Response> {
- return this.http.post(`http://localhost:58300/connect/token`,
- this.authService.authFormBody(model),
- {headers: this.authService.authHeaders()});
- }
- authHeaders(): Headers {
- const headers = new Headers(
- {
- 'Content-Type': 'application/x-www-form-urlencoded'
- });
- return headers;
- }
- authFormBody(model: ILoginModel): string {
- let body = '';
- body += 'grant_type=password$';
- body += 'username=' + model.email + '&';
- body += 'password=' + model.password + '&';
- body += 'scope=OpenId profile OfflineAccess Roles';
- return body;
- }
Add Comment
Please, Sign In to add comment