// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(
                                                             Configuration.GetConnectionString("DefaultConnection")));

            services.AddDefaultIdentity <IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores <ApplicationDbContext>();

            services.AddControllersWithViews();
            services.AddRazorPages();


            #region "JWT Token For Authentication Login"
            SiteKeys.Configure(Configuration.GetSection("AppSettings"));
            var key = Encoding.ASCII.GetBytes(SiteKeys.Token);

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(60);
            });


            services.AddAuthentication(auth =>
            {
                auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                auth.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(token =>
            {
                token.RequireHttpsMetadata      = false;
                token.SaveToken                 = true;
                token.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = true,
                    ValidIssuer           = SiteKeys.WebSiteDomain,
                    ValidateAudience      = true,
                    ValidAudience         = SiteKeys.WebSiteDomain,
                    RequireExpirationTime = true,
                    ValidateLifetime      = true,
                    ClockSkew             = TimeSpan.Zero
                };
            });

            #endregion
        }
Exemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            SQLHandler.Connectionconfig = _configuration.GetSection("ConnectionStrings").GetSection("TNPLConnectionString").Value;

            services.AddScoped <IUserServices, UserServices>();

            #region "USER AUTHENTICATION AND AUTHORIZATION"

            SiteKeys.Configure(_configuration.GetSection("Jwt"));

            var Key = Encoding.ASCII.GetBytes(SiteKeys.Key);

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(60);
            });

            services.AddAuthentication(auth =>
            {
                auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                auth.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(token =>
            {
                token.RequireHttpsMetadata      = false;
                token.SaveToken                 = true;
                token.TokenValidationParameters = JwtHelper.tokenValidationParameters;
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy(Policies.Admin, Policies.AdminPolicy());
                options.AddPolicy(Policies.User, Policies.UserPolicy());
            });

            #endregion
        }
Exemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddOptions();
            AppSetting.SecureAppUrl     = Configuration["profiles:" + _hostingEnvironment.EnvironmentName.ToLower() + ":SecureAppUrl"];
            AppSetting.ConnectionString = Configuration["profiles:" + _hostingEnvironment.EnvironmentName.ToLower() + ":ConnectionStrings:DefaultConnection"];
            services.AddDbContext <ApplicationDbContext>(options =>
            {
                options.UseSqlServer(AppSetting.ConnectionString);
            });

            services.AddScoped <IPasswordHasher <ApplicationUser>, SQLPasswordHasher>();
            //services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
            //services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddIdentity <ApplicationUser, ApplicationRole>(options =>
            {
                options.Password.RequireDigit           = false;
                options.Password.RequiredLength         = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireLowercase       = false;
                options.User.AllowedUserNameCharacters  = "()#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+/ $%^*!`~^&=[]{}\"';:,<.>?";
            })
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();
            services.AddSingleton <IConfiguration>(Configuration);
            services.AddScoped <IRepository, RepositoryService>();
            services.AddHttpContextAccessor();
            //ConfigureJwtAuth(services);
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie();
            //.AddCookie(options =>
            //{
            //    options.Cookie.HttpOnly = true;
            //    options.Cookie.SecurePolicy = _hostingEnvironment.IsDevelopment()
            //      ? CookieSecurePolicy.None : CookieSecurePolicy.Always;
            //    options.Cookie.SameSite = SameSiteMode.Lax;
            //});
            //services.Configure<CookiePolicyOptions>(options =>
            //{
            //    options.MinimumSameSitePolicy = SameSiteMode.Strict;
            //    options.HttpOnly = HttpOnlyPolicy.None;
            //    options.Secure = _hostingEnvironment.IsDevelopment()
            //      ? CookieSecurePolicy.None : CookieSecurePolicy.Always;
            //});
            services.AddControllersWithViews();
            //  services.AddMvc(options => options.EnableEndpointRouting = false);

            #region "JWT Token For Authentication Login"
            SiteKeys.Configure(Configuration.GetSection("AppSettings"));
            var key = Encoding.ASCII.GetBytes(SiteKeys.Token);

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(60);
            });


            services.AddAuthentication(auth =>
            {
                auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                auth.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(token =>
            {
                token.RequireHttpsMetadata      = false;
                token.SaveToken                 = true;
                token.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = true,
                    ValidIssuer           = SiteKeys.WebSiteDomain,
                    ValidateAudience      = true,
                    ValidAudience         = SiteKeys.WebSiteDomain,
                    RequireExpirationTime = true,
                    ValidateLifetime      = true,
                    ClockSkew             = TimeSpan.Zero
                };
            });

            #endregion
            services.AddSignalR();
        }