public void TestThatGetSymmetricSecurityKeyLoadsKeyFromConfiguration()
        {
            //arrange
            var sut = new JwtTokenManager(authSettingsOption, userManager);

            //act
            sut.GetSymmetricSecurityKey();
            //assert
            A.CallTo(() => authenticationSettings.SymmetricSecurityKey).MustHaveHappened();
        }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Set connection to database.
            services.AddDbContext <AppDbContext>(options =>
            {
                options.UseLazyLoadingProxies();
                options.UseSqlServer(Configuration.GetConnectionString("LocalDbConnection"));
            });

            // Adds and configures the identity system for the specified User and Role types.
            services.AddIdentity <HeroEntity, IdentityRole>(opts =>
            {
                opts.User.RequireUniqueEmail         = false;
                opts.Password.RequiredLength         = 6;
                opts.Password.RequireNonAlphanumeric = false;
                opts.Password.RequireLowercase       = false;
                opts.Password.RequireUppercase       = false;
                opts.Password.RequireDigit           = false;
            })
            .AddEntityFrameworkStores <AppDbContext>()
            .AddDefaultTokenProviders();

            // Add authentication services.
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = JwtTokenManager.ISSUER,
                    ValidAudience    = JwtTokenManager.AUDIENCE,
                    IssuerSigningKey = JwtTokenManager.GetSymmetricSecurityKey(),
                };
            });

            // Automapper
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            #region Dependency registration.

            // Repositories
            services.AddTransient(typeof(IGenericRepository <,>), typeof(GenericRepository <,>));
            services.AddTransient <IDragonRepository, DragonRepository>();
            services.AddTransient <IHeroRepository, HeroRepository>();
            services.AddTransient <IHitRepository, HitRepository>();

            // Services
            services.AddTransient <IDragonService, DragonService>();
            services.AddTransient <IHeroService, HeroService>();
            services.AddTransient <IHitService, HitService>();


            // Managers
            services.AddTransient(typeof(ITokenManager <HeroEntity, string>), typeof(JwtTokenManager));
            services.AddSingleton <IDataAdapter, DataAdapter>();

            #endregion


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version     = "v1",
                    Title       = "Heroes And Dragons",
                    Description = "API Sample"
                });
                c.AddSecurityDefinition("Bearer",
                                        new ApiKeyScheme
                {
                    In          = "header",
                    Description = "Please enter into field the word 'Bearer' following by space and JWT",
                    Name        = "Authorization",
                    Type        = "apiKey"
                });
                c.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> > {
                    { "Bearer", Enumerable.Empty <string>() },
                });
            });
        }