// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { string connection = Configuration.GetConnectionString(ConnectionStringName); services.AddDbContext <UsersDataProvider>(options => options.UseSqlServer(connection)); services.AddScoped <IUserDataProvider, UsersDataProvider>(); services.AddScoped(typeof(UsersDataProvider)); services.AddScoped <ILogging, CurrentLogger>(); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = AuthorizationOptions.Issuer, ValidateAudience = true, ValidAudience = AuthorizationOptions.Audience, ValidateLifetime = true, IssuerSigningKey = AuthorizationOptions.GetSymmetricSecurityKey(), ValidateIssuerSigningKey = true, }; }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Version = "v1", Title = "EHospital", Description = "Authorization for eHospital Project", TermsOfService = "Welcome everybody!", Contact = new Swashbuckle.AspNetCore.Swagger.Contact() { Name = "Julia Kropivnaya", Email = "*****@*****.**" } }); }); services.AddMvc(); }
/// <summary> /// Get new token /// </summary> /// <param name="username">login</param> /// <returns>token</returns> private async Task <string> GetToken(string username) { int userId = await _appDbContext.FindByLogin(username); var identity = await _authorizationManager.GetIdentity(username, userId); if (identity == null) { return(null); } _log.LogInfo("Set token options."); var now = DateTime.Now; var jwt = new JwtSecurityToken( issuer: AuthorizationOptions.Issuer, audience: AuthorizationOptions.Audience, notBefore: now, claims: identity.Claims, expires: now.Add(TimeSpan.FromMinutes(AuthorizationOptions.Lifetime)), signingCredentials: new SigningCredentials(AuthorizationOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); _log.LogInfo("Set session options."); Sessions start = new Sessions() { Token = encodedJwt, UserId = userId, ExpiredDate = now.Add(TimeSpan.FromMinutes(AuthorizationOptions.Lifetime)) }; _log.LogInfo("Check for previous session."); if (await _appDbContext.IsExistPreviousSession(userId)) { _log.LogInfo("The session was founded. I`ll delete it."); await _appDbContext.DeleteSessions(userId); _log.LogInfo("Success delete."); } _log.LogInfo("Add session"); await _appDbContext.AddSession(start); _log.LogInfo("Session was add."); _log.LogInfo("Return session's token"); return(encodedJwt); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = AuthorizationOptions.ISSUER, ValidAudience = AuthorizationOptions.AUDIENCE, IssuerSigningKey = AuthorizationOptions.GetSymmetricSecurityKey(), ValidateLifetime = true, ValidateIssuerSigningKey = true, ClockSkew = TimeSpan.Zero }; }); var mapperConfig = new MapperConfiguration(mc => { mc.AddProfile(new MappingProfile()); }); var mapper = mapperConfig.CreateMapper(); services.AddSingleton(mapper); services.AddCors(); services.AddMvc(mvcOptions => mvcOptions.EnableEndpointRouting = false) .SetCompatibilityVersion(CompatibilityVersion.Latest); services.AddScoped <IRepositoryContextFactory, AdaruDBContextFactory>(); services.AddScoped <IChatRepository>(provider => new ChatRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IClientRepository>(provider => new ClientRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <ICustomerRepository>(provider => new CustomerRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IImageRepository>(provider => new ImageRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IInviteRepository>(provider => new InviteRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IMessageRepository>(provider => new MessageRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IPerformerRepository>(provider => new PerformerRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IProfileRepository>(provider => new ProfileRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IReviewRepository>(provider => new ReviewRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IRoleRepository>(provider => new RoleRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IStatusRepository>(provider => new StatusRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <ITagRepository>(provider => new TagRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <ITaskInfoRepository>(provider => new TaskInfoRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <ITaskRepository>(provider => new TaskRepository(Configuration.GetConnectionString("DefaultConnection"), provider.GetService <IRepositoryContextFactory>())); services.AddScoped <IImageService, ImageService>(); }