Exemple #1
0
        public int GetAccessTokenUserId(string token)
        {
            var Token = Guard.Argument(token, nameof(token)).NotEmpty().NotNull().NotWhiteSpace();

            return(int.Parse(GetClaimValue(ClaimTypes.Name,
                                           _jwtSettings.GetTokenValidationParameters(_jwtSettings.AccessTokenSecret), Token)));
        }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();

            services.AddTransient <JWTSettings>();
            services.AddTransient <IAuthService, AuthService>();
            services.AddTransient <IJWTService, JWTService>();
            services.AddTransient <IUserService, UserService>();
            services.AddTransient <IClientService, ClientService>();
            services.AddTransient <IPhotoService, PhotoService>();
            services.AddTransient <IFurnitureService, FurnitureService>();
            services.AddTransient <IShoppingCartService, ShoppingCartService>();
            services.AddTransient <ICustomSizeService, CustomSizeService>();



            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;
            });

            var connectionString = "server=" + System.Environment.GetEnvironmentVariable("DATABASE_HOST") +
                                   ";userid=" + System.Environment.GetEnvironmentVariable("DATABASE_USER") +
                                   ";password="******"DATABASE_PASSWORD") +
                                   ";database=" + System.Environment.GetEnvironmentVariable("DATABASE_NAME");

            services.AddDbContext <MeblexDbContext>(options =>
            {
                options.UseLazyLoadingProxies();
                options.UseMySql(connectionString);
            });



            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters =
                    _jwtSettings.GetTokenValidationParameters(_jwtSettings.AccessTokenSecret);
            });


            services.AddAutoMapper(cfg =>
            {
                cfg.ForAllMaps((typeMap, map) =>
                {
                    map.ForAllMembers(option =>
                                      option.Condition((source, destination, sourceMember) => sourceMember != null));
                });
                cfg.ValidateInlineMaps    = false;
                cfg.CreateMissingTypeMaps = true;
            });



            ValidatorOptions.PropertyNameResolver    = (type, info, arg3) => info.Name.ToLower();
            ValidatorOptions.CascadeMode             = CascadeMode.Continue;
            ValidatorOptions.LanguageManager.Enabled = true;
            services.AddOData();
            services.AddLocalization(options => options.ResourcesPath = "Resources");
            services.AddMvc(options =>
            {
                foreach (var outputFormatter in options.OutputFormatters.OfType <ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
                foreach (var inputFormatter in options.InputFormatters.OfType <ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
                {
                    inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
                }
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddFluentValidation(fv =>
            {
                fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;

                fv.RegisterValidatorsFromAssemblyContaining <Startup>();
            })
            .AddJsonOptions(opt => opt.SerializerSettings.StringEscapeHandling = StringEscapeHandling.EscapeHtml);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info()
                {
                    Title = "Meblex API", Version = "v1"
                });
                c.AddFluentValidationRules();
                c.EnableAnnotations();
                c.DocumentFilter <MiscFunctions.CustomModelDocumentFilter <PieceOfFurnitureAddForm> >();
                c.DocumentFilter <MiscFunctions.CustomModelDocumentFilter <PatternAddForm> >();
                c.DocumentFilter <MiscFunctions.CustomModelDocumentFilter <MaterialAddForm> >();
            });

            services.Configure <RequestLocalizationOptions>(opt =>
            {
                var supportedCultures     = new[] { new CultureInfo("en"), new CultureInfo("pl") };
                opt.DefaultRequestCulture = new RequestCulture("pl", "pl");
                opt.SupportedCultures     = supportedCultures;
                opt.SupportedUICultures   = supportedCultures;
                opt.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
                {
                    var lang = context.Request.GetTypedHeaders().AcceptLanguage?.FirstOrDefault()?.Value.Value ?? "en-US";
                    return(Task.FromResult(new ProviderCultureResult(lang, lang)));
                }));
            });
        }