예제 #1
0
        public static void ConfigurAuthentication(this IServiceCollection serviceCollection, IConfiguration configuration, bool isDevelopment)
        {
            var audiences = configuration["Auth:Audience"].Split(',');

            var signingKey    = new SignInSymmetricKey(configuration["TokenOptions:Key"]);
            var decryptionKey = new JwtCrypt(configuration["TokenOptions:CypherKey"]);

            var signingDecodingKey = (IJwtSigningDecodingKey)signingKey;
            var decryptKey         = (IJwtEncryptingDecodingKey)decryptionKey;

            serviceCollection
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata      = false;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer              = configuration["Auth:Issuer"],
                    ValidAudiences           = audiences,
                    IssuerSigningKey         = signingDecodingKey.GetKey(),
                    TokenDecryptionKey       = decryptKey.GetKey(),
                    ClockSkew                = TimeSpan.Zero,
                    ValidateLifetime         = true,
                    ValidateAudience         = false,
                    ValidateIssuer           = true,
                    ValidateIssuerSigningKey = true
                };

                cfg.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context =>
                    {
                        var accessToken = context.Request.Query["access_token"];
                        var path        = context.HttpContext.Request.Path;

                        if (!string.IsNullOrEmpty(accessToken))
                        {
                            context.Token = accessToken;
                        }
                        return(Task.CompletedTask);
                    }
                };
            });
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            #region Auth
            const string signingSecurityKey = "0d5b3235a8b403c3dab9c3f4f65c07fcalskd234n1k41230";
            var          signingKey         = new SignInSymmetricKey(signingSecurityKey);
            services.AddSingleton <IJwtSigningEncodingKey>(signingKey);

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

            const string jwtSchemeName      = "JwtBearer";
            var          signingDecodingKey = (IJwtSigningDecodingKey)signingKey;
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = jwtSchemeName;
                options.DefaultChallengeScheme    = jwtSchemeName;
            }).AddJwtBearer(jwtSchemeName, jwtBearerOptions =>
            {
                jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = signingDecodingKey.GetKey(),

                    ValidateIssuer = true,
                    ValidIssuer    = "Unotator",

                    ValidateAudience = true,
                    ValidAudience    = "UnotatorClient",

                    ValidateLifetime = true,

                    ClockSkew = TimeSpan.FromSeconds(5)
                };
            });
            #endregion

            #region DB Context
            string connectionString = Configuration.GetConnectionString("AppDb");
            services.AddDbContext <Infrasructure.Data.AppContext>(m => m.UseSqlServer(connectionString));
            #endregion

            #region Infrastructure
            services.AddTransient <IRepository <User>, EFRepository <User> >();
            services.AddTransient <IRepository <Topic>, EFRepository <Topic> >();
            services.AddTransient <IRepository <Entry>, EFRepository <Entry> >();
            services.AddTransient <IUnitOfWork, UnitOfWork>();
            #endregion

            #region Application Services
            services.AddTransient <IUserService, UserService>();
            services.AddTransient <ITopicService, TopicService>();
            services.AddTransient <IEntryService, EntryService>();
            #endregion

            #region AutoMapper
            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new Map());
            });
            IMapper mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);
            #endregion

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddSpaStaticFiles(options => options.RootPath = "client-app/dist");
        }