예제 #1
0
        public TokenDto CreateToken(Cavus cavus)
        {
            var AccesTokenOmru   = DateTime.Now.AddMinutes(_customTokenOptions.AccesTokenO);                                    //Token ömrünü al
            var RefreshTokenOmru = DateTime.Now.AddMinutes(_customTokenOptions.RefreshTokenO);                                  //Refresh token ömrünü al
            var SecuritKey       = CustomSecurity.GetSymetricSecurityKey(_customTokenOptions.SecuritKey);
            SigningCredentials signingCredentials = new SigningCredentials(SecuritKey, SecurityAlgorithms.HmacSha256Signature); //imzamızı oluşturuyoruz
            JwtSecurityToken   jwtSecurityToken   = new JwtSecurityToken(

                issuer: _customTokenOptions.Issuer,
                expires: AccesTokenOmru,
                notBefore: DateTime.Now,
                claims: GetClaim(cavus, _customTokenOptions.Audience),
                signingCredentials: signingCredentials);

            var handler = new JwtSecurityTokenHandler();
            var token   = handler.WriteToken(jwtSecurityToken);

            var tokenDto = new TokenDto
            {
                AccessToken          = token,
                RefreshToken         = CreateRefreshToken(),
                AccesTokenLifeTime   = AccesTokenOmru,
                RefreshTokenLifeTime = RefreshTokenOmru
            };

            return(tokenDto);
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped <IAuthenticationService, AuthenticationService>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IUcretService, UcretService>();
            services.AddScoped <IIsciService, IsciService>();
            services.AddScoped <IAileService, AileService>();
            services.AddScoped <IGrupService, GrupService>();
            services.AddScoped <IGiderService, GiderService>();
            services.AddScoped <IIsIsciService, IsIsciService>();
            services.AddScoped(typeof(ISumService <>), typeof(SumService <>));
            services.AddScoped <IIsService, IsService>();
            services.AddScoped <IIsverenService, IsverenService>();
            services.AddScoped <ITokenService, CustomTokenService>();
            services.AddScoped(typeof(IGenericRepository <>), typeof(GenericRepository <>));
            services.AddScoped(typeof(IServiceGeneric <,>), typeof(ServicesGeneric <,>));
            services.AddScoped <IUnitOfWork, UnitOfWork>();
            //FluentValidation Serrvisini AddFluetValidation ile uygulamaya entegre edecez
            //Entitlere gelen validationlarýn nerde tututulduðunu sisteme bildirmemiz lazým
            //RegisterValidatorsFromAssemblyContaining içinde tanýmladýðýmýz sýnýf neyse o sýnýfýn içinde bulunduðu
            //Asembly bulup o asembly içerisindeki tüm validater larý bulup sisteme entegre edicek
            services.AddControllers().AddFluentValidation(x => x.RegisterValidatorsFromAssemblyContaining <Startup>());
            services.AddDbContext <ApplicationDbContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString"), sqlOptions =>
                {
                    sqlOptions.MigrationsAssembly("ITS.DATA");
                });
            });
            services.AddIdentity <Cavus, IdentityRole>(opt => {
                opt.Password.RequiredLength         = 4;
                opt.Password.RequireNonAlphanumeric = false;
                opt.Password.RequireUppercase       = false;
                opt.Password.RequireLowercase       = false;
                opt.Password.RequireDigit           = false;
            }
                                                       ).AddEntityFrameworkStores <ApplicationDbContext>();

            services.Configure <CustomTokenOptions>(Configuration.GetSection("TokenAyar"));

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, opts =>
            {
                var tokenOptionss = Configuration.GetSection("TokenAyar").Get <CustomTokenOptions>();
                opts.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                {
                    ValidIssuer      = tokenOptionss.Issuer,
                    ValidAudience    = tokenOptionss.Audience[0],
                    IssuerSigningKey = CustomSecurity.GetSymetricSecurityKey(tokenOptionss.SecuritKey),

                    ValidateIssuerSigningKey = true,
                    ValidateAudience         = true,
                    ValidateIssuer           = true,
                    ClockSkew = TimeSpan.Zero
                };
            });
        }