public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { _accessTokenExpiration = DateTime.Now.AddMinutes(_tokenOptions.AccessTokenExpiration); var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); //jwt nesnesini writetoken methoduyla stringe çevirdik. var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
public TAccessToken CreateToken <TAccessToken>(User user) where TAccessToken : IAccessToken, new() { _accessTokenExpiration = DateTime.Now.AddMinutes(_tokenOptions.AccessTokenExpiration); var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new TAccessToken() { Token = token, Expiration = _accessTokenExpiration }); }
public AccessToken CreateToken(User user) { var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return new AccessToken { Token = token, Expiration = _accessTokenExpiration, UserId = user.Id }; }
public AccessToken CreateToken(Users user, List <UserClaims> claims, string dataBaseName) { var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialHelper.CreateSigningCredentials(securityKey); _accessTokenExpiration = DateTime.Now.AddMinutes(_tokenOptions.AccessTokenExpiration); var jwt = CreateJwtSecurityToken(tokenOptions: _tokenOptions, user: user, signingCredentials: signingCredentials, userClaims: claims, dataBaseName); var JwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = JwtSecurityTokenHandler.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new AccessToken { UserId = user.Id, Token = token, Expiration = _accessTokenExpiration, UserName = user.Name + " " + user.LastName }); }
public TokenModel CreateToken(User user) { _TokenModelExpiration = DateTime.Now.AddMinutes(_tokenOptions.AccessTokenExpiration); var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new TokenModel { Id = user.Id, Auth_Token = token, Expires_In = _TokenModelExpiration }); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { //securityKey : helps to create a token var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //AOP //Autofac, Ninject,CastleWindsor, StructureMap, LightInject, DryInject -->IoC Container //AOP //Postsharp services.AddControllers(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); //ServiceTool.Create(services); services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); //services.AddControllers(); //services.AddSingleton<ICarService, CarManager>(); //services.AddSingleton<ICarDal, EfCarDal>(); //services.AddSingleton<IColorService, ColorManager>(); //services.AddSingleton<IColorDal, EfColorDal>(); //services.AddSingleton<ICustomerService, CustomerManager>(); //services.AddSingleton<ICustomerDal, EfCustomerDal>(); //services.AddSingleton<IBrandService, BrandManager>(); //services.AddSingleton<IBrandDal, EfBrandDal>(); //services.AddSingleton<IRentalService, RentalManager>(); //services.AddSingleton<IRentalDal, EfRentalDal>(); //services.AddSingleton<IUserService, UserManager>(); //services.AddSingleton<IUserDal, EfUserDal>(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddCors(); //services.AddSingleton<ICarService,CarManager>(); //içinde data tutulmuyorsa singleton kullanýlýyor. //services.AddSingleton<ICarDal,EfCarDal>(); //services.AddSingleton<IBrandService, BrandManager>(); //services.AddSingleton<IBrandDal, EfBrandDal>(); //services.AddSingleton<IColorService, ColorManager>(); //services.AddSingleton<IColorDal, EfColorDal>(); //services.AddSingleton<ICustomerService, CustomerManager>(); //services.AddSingleton<ICustomerDal, EfCustomerDal>(); //services.AddSingleton<IUserService, UserManager>(); //services.AddSingleton<IUserDal, EfUserDal>(); //services.AddSingleton<IRentalService, RentalManager>(); //services.AddSingleton<IRentalDal, EfRentalDal>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); //bunu kullanmak için IServiceCollection ý extent edeceðiz yani içine AddDependencyResolvers yazarak geniþleteceðiz services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); //farklý modüller oluþturursak buraya onu da ekleyebiliriz }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSwaggerGen(c => { c.SwaggerDoc("WebAPI", new OpenApiInfo // Bu k�s�mda d�k�manda kullan�lacak bilgileri tan�ml�yoruz.Versiyon,Ba�l�k,A��klama,Servis gibi bilgileri yazabiliriz. { //Burada dikkat edilmesi gereken konu yukar�da parametre olarak ge�irdi�imizi "ProductApi". Burada verdi�iniz de�er ile a�a��da configure i�erisinde swagger�n json dosyas�n�n pathini verirken kulland���m�z de�er ayn� olmal� Version = "v1", Title = "WebAPI", Description = "A simple example ASP.NET Core Web API", TermsOfService = new Uri("https://example.com/terms"), Contact = new OpenApiContact { Name = "Muhammet Bilgin", Email = "*****@*****.**", Url = new Uri("https://www.google.com"), }, }); }); //AOP //Autofac, Ninject,CastleWindsor, StructureMap, LightInject, DryInject -->IoC Container //AOP //Postsharp services.AddControllers(); //services.AddSingleton<IProductService,ProductManager>(); //services.AddSingleton<IProductDal, EfProductDal>(); services.AddCors(); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddCors(); //services.AddSingleton<IColorService, ColorManager>(); //services.AddSingleton<IColorDal, EfColorDal>(); //services.AddSingleton<IBrandService, BrandManager>(); //services.AddSingleton<IBrandDal, EfBrandDal>(); //services.AddSingleton<ICarService, CarManager>(); //services.AddSingleton<ICarDal, EfCarDal>(); //services.AddSingleton<ICustomerService, CustomerManager>(); //services.AddSingleton<ICustomerDal, EfCustomerDal>(); //services.AddSingleton<IUserService, UserManager>(); //services.AddSingleton<IUserDal, EfUserDal>(); //services.AddSingleton<IRentalService, RentalManager>(); //services.AddSingleton<IRentalDal, EfRentalDal>(); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <remarks> /// Tüm konfigürasyonlar için ortaktır ve çağırılması gerekir. Aspnet core diğer /// metotlar olduğu için bu metodu çağırmaz. /// </remarks> /// <param name="services"></param> public override void ConfigureServices(IServiceCollection services) { // Business katmanında olan dependency tanımlarının bir metot üzerinden buraya implemente edilmesi. services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); options.JsonSerializerOptions.IgnoreNullValues = true; }); services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); }); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey), ClockSkew = TimeSpan.Zero }; }); services.AddSwaggerGen(c => { c.IncludeXmlComments(Path.ChangeExtension(typeof(Startup).Assembly.Location, ".xml")); }); services.AddTransient <FileLogger>(); services.AddTransient <PostgreSqlLogger>(); base.ConfigureServices(services); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // Autofac/Ninject/CastleWindsor/StructureMap/LightInject/DryInject for IoC Container Architecture // Autofac is used in this project /* Neden bu yapýldý? - Ýleride birden fazla API eklenirse, farklý servis yapýlarý mimarileri * eklenirse tüm yapýlandýrma ayarlarý bu Startup sýnýfýnda kalýr. * Tekrar tekrar yazýlamasýnýn önlenmesi için Business'a eklenerek kullanýma açýk hale * getirilmelidir. * Dependency Resolvers: Loosely couple baðýlýlýðý(interface injection) çözümleme iþlemleri. * .Net'in IoC yapýlandýrýlmasý kullanýlmadýðý için AutofacBusinessModule olarak yazýlan * IoC Container kullanýlacak. */ // Every request is percieved a threat by the system come to API. To allow them: services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.WithOrigins("http://localhost:3000")); }); // JWT Configuration: var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); // The AddDependendyResolvers structure was established for the CoreModule added here and the modules to be added in the future. services.AddDependencyResolvers(new ICoreModule[] { // params can be used too new CoreModule() }); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { //token oluştururken çeşitli bilgiler lazım.bunlardan biri securitykey.Algoritmayı kullanarak token oluşturucaz şifreli.tokenı oluştururken encript ederken bir anahtara ihtiyacımız var onu kullanıyor olacağız _accessTokenExpiration = DateTime.Now.AddMinutes(_tokenoptions.AccessTokenExpiration); var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenoptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenoptions, user, signingCredentials, operationClaims); var jwtsecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtsecurityTokenHandler.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
public AccessToken CreateTokenForLogin(User user, List <UserRoleClaimsJoin> roleClaims, List <UserClaim> userClaims, bool rememberMe) { _accessTokenExpiration = rememberMe ? DateTime.Now.AddYears(5) : DateTime.Now.AddMinutes(_tokenOptions.AccessTokenExpiration); var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwtSecurityToken = CreateJwtSecurityTokenForLogin(_tokenOptions, user, signingCredentials, roleClaims, userClaims); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwtSecurityToken); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //services.AddSingleton<ICarService, CarManager>(); //services.AddSingleton<ICarDal, EfCarDal>(); //services.AddSingleton<IBrandService, BrandManager>(); //services.AddSingleton<IBrandDal, EfBrandDal>(); //services.AddSingleton<IColorService, ColorManager>(); //services.AddSingleton<IColorDal, EfColorDal>(); //services.AddSingleton<ICustomerService, CustomerManager>(); //services.AddSingleton<ICustomerDal, EfCustomerDal>(); //services.AddSingleton<IRentalService, RentalManager>(); //services.AddSingleton<IRentalDal, EfRentalDal>(); //services.AddSingleton<IUserService, UserManager>(); //services.AddSingleton<IUserDal, EfUserDal>(); //yukardaki ioc conteiner iþlemlerini autofac ile gerçekleþtirdik services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddCors(); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); services.AddSwaggerGen((c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); })); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // services.AddAutoMapper(typeof(Startup)); //services.AddDbContext<PostgresqlContext>(options => // options.UseNpgsql( // _configuration.GetConnectionString("DefaultConnection"),x=>x.MigrationsAssembly("WebUI"))); services.InstallServicesInAssembly(Configuration); var tokenOptions = _configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); var redisCacheSettings = new RedisCacheSettings(); _configuration.GetSection("RedisCacheSettings").Bind(redisCacheSettings); services.AddSingleton(redisCacheSettings); if (!redisCacheSettings.Enabled) { return; } services.AddStackExchangeRedisCache(options => options.Configuration = redisCacheSettings.ConnectionString); services.AddSingleton <IRedisCacheService, RedisApiCache>(); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "Cooks API", Version = "V1" }); }); services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.WithOrigins("https://localhost:5000")); }); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterModule <AutofacBussinessModule>(); containerBuilder.Populate(services); var container = containerBuilder.Build(); services.AddDependencyResolver(new ICoreModule[] { new CoreModule() }); return(new AutofacServiceProvider(container)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddNewtonsoftJson(); // services.AddControllers(); //services.AddSingleton<ICarManager,CarManager>(); //services.AddSingleton<ICarDal,EfCarDal>(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "RentaCar API", Version = "v1" }); }); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); services.AddCors(options => { options.AddPolicy(allowSpecificOrigins, builder => { builder.WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod(); }); }); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) //token optionsu okumak olucak amaç { var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); //token oluşturucaz encrypt oluşuturuken anahtar lazım //new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenoptions.SecurityKey); var signingCredentials = SigningCredentialsHelper.CreateCredentials(securityKey); var jwt = CreatJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims); //bizde tokun mevcut ama biz o tokeni elimizdeki bilgilere göre handlerle(işleyici) yazmamız gerek. var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt);//token stringe çevrildi return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddCors(); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); //Autofac desteðini eklediðimiz için bu kodlarý iptal ettik. IoC yapýlanmasýný Autofac ile gerçekleþtirdik #region CanseledCodes //services.AddSingleton<ICarService,CarManager>(); //services.AddSingleton<ICarDal, EfCarDal>(); //services.AddSingleton<IBrandService, BrandManager>(); //services.AddSingleton<IBrandDal, EfBrandDal>(); //services.AddSingleton<IRentalService, RentalManager>(); //services.AddSingleton<IRentalDal, EfRentalDal>(); //services.AddSingleton<IColorService, ColorManager>(); //services.AddSingleton<IColorDal, EfColorDal>(); //services.AddSingleton<ICustomerService, CustomerManager>(); //services.AddSingleton<ICustomerDal, EfCustomerDal>(); //services.AddSingleton<IModelService, ModelManager>(); //services.AddSingleton<IModelDal, EfModelDal>(); //services.AddSingleton<IUserService, UserManager>(); //services.AddSingleton<IUserDal, EfUserDal>(); #endregion //ServiceTool.Create(services); services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //Bana arka planda bir referans olustur dedik. ICarService gorursen o CarManager. Arka planda newler. //Data tutmuyorsan singleton uygula. //services.AddSingleton<ICarService, CarManager>(); //Yukarýdakini yapmaya gitti dediki abi dedi ben gittim CarManager e ama oda ICarDal a bagýmlý. //Ondan burada ICarDal gorursen bilki EfCarDal o dedik. Arka planda newlendi yani. //services.AddSingleton<ICarDal, EfCarDal>(); //services.AddSingleton<IBrandService, BrandManager>(); //services.AddSingleton<IBrandDal, EfBrandDal>(); //services.AddSingleton<IColorService, ColorManager>(); //services.AddSingleton<IColorDal, EfColorDal>(); //services.AddSingleton<ICategoryService, CategoryManager>(); //services.AddSingleton<ICategoryDal, EfCategoryDal>(); //services.AddSingleton<ICustomerService, CustomerManager>(); //services.AddSingleton<ICustomerDal, EfCustomerDal>(); //services.AddSingleton<IRentalService, RentalManager>(); //services.AddSingleton<IRentalDal, EfRentalDal>(); //services.AddSingleton<IUserService, UserManager>(); //services.AddSingleton<IUserDal, EfUserDal>(); services.AddCors(); //Istek gelic authun devreye girmesi icin. //TokenOptionsu okuduk. var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); //Bir c*k modul eklemek istiyoruz. //Yarýn obur gun coremodule gibi farklý moduller olusturursak istedigimiz kadarýný ekliyebiliriz. services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { // tokenı oluştururken kendi bildiğimiz özel anahtar var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var signinCredentials = SigningCredentialsHelper.CreaterSigningCredentials(securityKey); var jwt = CreateJwtSecurityToken(_tokenOptions, user, signinCredentials, operationClaims); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); //Encryption içindeki sınıfı ve metodu kullanarak bize bir security key oluştur diyoruz.Yani bir anahtar oluyor //Şimdi Signing Credential helper la devam eidyoruz. Yine Encryption altında SigningCredentialHelper ı yazıyoruz. var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); // var jwt = CreateJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHandler.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExpiration }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Quiz API", Version = "v1" }); }); services.AddControllers(); services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.WithOrigins("https://localhost:44351")); }); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); services.AddDependencyResolvers(new Core.Utilities.IoC.ICoreModule[] { new CoreModule() }); services.Configure <IISServerOptions>(options => { options.AutomaticAuthentication = false; }); services.Configure <IISOptions>(options => { options.ForwardClientCertificate = false; }); }
// This method gets called by the runtime. Use this method to add services to the container. /// <summary> /// /// </summary> /// <param name="services"></param> public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); }); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); services.AddSwaggerGen(c => { c.IncludeXmlComments(Path.ChangeExtension(typeof(Startup).Assembly.Location, ".xml")); // Bu metot yeni. Enumları inline olarak kodluyor. c.UseInlineDefinitionsForEnums(); }); services.AddMediatR(Assembly.GetAssembly(typeof(SecuredOperation))); // .Net 3.0 sonrası container üzerinden build çağırMAMAmız gerekiyormuş. // ServiceTool un provider'i aspectlerde kullanildigi icin bir yerde set etmemiz gerekiyor. // Bunu da Configure Services'de yapiyoruz. services.AddDependencyResolvers(Configuration, new ICoreModule[] { new BusinessModule(), new CoreModule() }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => builder.WithOrigins("http://localhost:3000")); }); services.AddAuthentication(options => { options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; }); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }) /*.AddTwitter(twitterOptions => * { * twitterOptions.ConsumerKey = ""; * twitterOptions.ConsumerSecret = ""; * * })*/ .AddFacebook(facebookOptions => { facebookOptions.AppId = "458241478223006"; facebookOptions.AppSecret = "29219c3d498a0e7da596f0628ac8f8c2"; }) //localhost:55466\signing-facebook .AddCookie(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); /* * services.addsingleton<ýcarservice, carmanager>(); * services.addsingleton<ýcardal, efcardal>(); * * services.addsingleton<ýbrandservice, brandmanager>(); * services.addsingleton<ýbranddal, efbranddal>(); * * services.addsingleton<ýcolorservice, colormanager>(); * services.addsingleton<ýcolordal, efcolordal>(); * * services.addsingleton<ýcustomerservice, customermanager>(); * services.addsingleton<ýcustomerdal, efcustomerdal>(); * * services.addsingleton<ýrentalservice, rentalmanager>(); * services.addsingleton<ýrentaldal, efrentaldal>(); * * services.addsingleton<ýuserservice, usermanager>(); * services.addsingleton<ýuserdal, efuserdal>(); */ services.AddCors(); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(tokenOptions.SecurityKey) }; }); services.AddDependencyResolvers(new ICoreModule[] { new CoreModule() }); }
public AccessToken CreateToken(User user, List <OperationClaim> operationClaims) { // TokenOptionsdan Security Key prop'una ulaştık ve bu fonksiyonda çağırdık. // Key'imizi çağırdık. var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); // Dijital imza oluşturmak için kullanılan şifreleme anahtarını ve güvenlik algoritmalarını temsil eder. var signingCredentials = SigningCredentialsHelper.CreateSigningCredentials(securityKey); var jwt = CreatJwtSecurityToken(_tokenOptions, user, signingCredentials, operationClaims); var jwtSecurityTokenHelper = new JwtSecurityTokenHandler(); var token = jwtSecurityTokenHelper.WriteToken(jwt); return(new AccessToken { Token = token, Expiration = _accessTokenExprission }); }
public int GetNameIdentifier(string token) { var securityKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey); var obj = _jwtSecurityTokenHandler.ValidateToken(token, new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = false, ValidIssuer = _tokenOptions.Issuer, ValidAudience = _tokenOptions.Audience, ValidateIssuerSigningKey = true, IssuerSigningKey = SecurityKeyHelper.CreateSecurityKey(_tokenOptions.SecurityKey), }, out var securityToken); var kullaniciId = int.Parse(obj.Claims.First(w => w.Type == ClaimTypes.NameIdentifier).Value); return(kullaniciId); }