示例#1
0
        // 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());
     }
 }
示例#3
0
        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());
            }
        }
示例#4
0
 public AuthRepository(JWTAuthenticate jwtAuthenticate)
 {
     this.JWTAuthenticate = jwtAuthenticate;
 }
示例#5
0
 public JwtRefreshTokenCache(JWTAuthenticate jwtAuthManager)
 {
     _jwtAuthManager = jwtAuthManager;
 }