// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { using (var db = new DataBase()) { var wasCreate = db.Database.EnsureCreated(); } services.AddEntityFrameworkSqlite(); var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get <JwtTokenConfig>(); if (jwtTokenConfig == null) { var path = Directory.GetCurrentDirectory() + "\\appsettings.json"; jwtTokenConfig = new ConfigurationBuilder() .AddJsonFile(path) .Build() .GetSection("jwtTokenConfig") .Get <JwtTokenConfig>(); } var jwtAuthenticate = new JWTAuthenticate(jwtTokenConfig); IAuthRepository authRepository = new AuthRepository(jwtAuthenticate); IUserRepository userRepository = new UserRepository(); ITaskRepository taskRepository = new TaskRepository(); IFileRepository fileRepository = new FileRepository(); services.AddTransient <TaskUsesCase>((_) => new TaskUsesCase(taskRepository)); services.AddTransient <UserUsesCase>((_) => new UserUsesCase(userRepository, fileRepository)); services.AddSingleton <AuthUsesCase>((_) => new AuthUsesCase(authRepository, userRepository)); services.AddRazorPages(); services.AddSingleton(jwtTokenConfig); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(x => { x.RequireHttpsMetadata = true; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = jwtTokenConfig.Issuer, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtTokenConfig.Secret)), ValidAudience = jwtTokenConfig.Audience, ValidateAudience = true, ValidateLifetime = true, ClockSkew = TimeSpan.FromMinutes(1) }; }); services.AddHostedService <JwtRefreshTokenCache>((_) => new JwtRefreshTokenCache(jwtAuthenticate)); }
public IHttpActionResult CheckToken(AuthorizationDTO authorization) { try { if (JWTAuthenticate.Instance().IsValid(authorization.IdToken)) { return(StatusCode(HttpStatusCode.NoContent)); } else { return(Unauthorized()); } } catch { return(InternalServerError()); } }
public async Task <IHttpActionResult> GetAccessToken([FromBody] CredentialDTO credential, [FromUri] string scope = null) { string userName = credential.UserName; string password = credential.Password; try { bool isValidUser = await Authentication.CheckAuthenticateAsync(userName, password, DOMAIN_NAME); if (!isValidUser) { return(Unauthorized()); } AuthorizationDTO authorization = null; if (string.Equals(scope, "none", StringComparison.OrdinalIgnoreCase)) { authorization = new AuthorizationDTO { IdToken = JWTAuthenticate.Instance().GetBasicToken(userName) }; } else { UserDetail user = await Authentication.GetUserEmailFromADAsync(userName, password, DOMAIN_NAME); authorization = new AuthorizationDTO { IdToken = JWTAuthenticate.Instance().GetDetailedToken(userName, user.EmailAddress, user.Name) }; } return(Ok(authorization)); } catch { return(InternalServerError()); } }
public AuthRepository(JWTAuthenticate jwtAuthenticate) { this.JWTAuthenticate = jwtAuthenticate; }
public JwtRefreshTokenCache(JWTAuthenticate jwtAuthManager) { _jwtAuthManager = jwtAuthManager; }