Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson(options =>

                                                        options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                                                        );

            services.AddDbContext <CreaFormDBcontext>
                (options => options.UseSqlServer(Configuration.GetConnectionString("Connst")));
            services.AddScoped <IAuthRepository, AuthRepository>();
            services.AddScoped <IAdminRepository, AdminRepository>();
            services.AddScoped <IAdvisorRepository, AdvisorRepository>();
            services.AddScoped <IClientRepository, ClientRepository>();
            services.AddScoped <ISymtomRepo, SymtomRepo>();

            services.AddTransient <SeedData>();
            services.AddAutoMapper(typeof(CreaFormProfile));

            //Add Identity
            services.AddIdentity <User, IdentityRole>(opt =>
            {
                opt.Password.RequireNonAlphanumeric = false;
                opt.Password.RequiredLength         = 5;
                opt.Password.RequireDigit           = true;
                opt.Password.RequireUppercase       = true;
            })
            .AddEntityFrameworkStores <CreaFormDBcontext>();

            //ADD Swagger
            services.AddSwaggerGen(Options =>
            {
                Options.SwaggerDoc("CreaFormOpenAPISpec", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Title   = "CreaForm API",
                    Version = "1",
                    Contact = new Microsoft.OpenApi.Models.OpenApiContact()
                    {
                        Email = "*****@*****.**",
                        Name  = "Bahaa Abo Khaled"
                    },
                    Description = "CreaForm API"
                });


                //För att visa beskrivningen relaterad till varje metod i kontrollen
                var Xmlcommentfile     = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlcommentFullPath = Path.Combine(AppContext.BaseDirectory, Xmlcommentfile);
                Options.IncludeXmlComments(xmlcommentFullPath);

                //JWT tokens inside a swagger.
                Options.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
                {
                    Description =
                        "Jwt Authorization header using the Bearer schema.\r\n\r\n Enter 'Bearer '[mellanslag] och sedan din token i textinmatningen nedan ",
                    Name   = "Authorization",
                    In     = Microsoft.OpenApi.Models.ParameterLocation.Header,
                    Type   = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });
                Options.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    { new OpenApiSecurityScheme
                      {
                          Reference = new OpenApiReference
                          {
                              Type = ReferenceType.SecurityScheme,
                              Id   = "Bearer"
                          },
                          Scheme = "oauth2",
                          Name   = "Bearer",
                          In     = ParameterLocation.Header,
                      },
                      new List <string>() }
                });
            });
            //add Authonticat

            var appsettingsection = Configuration.GetSection("Appsettings");

            services.Configure <Appsettings>(appsettingsection);
            var appsettings = appsettingsection.Get <Appsettings>();
            var Key         = Encoding.ASCII.GetBytes(appsettings.Secret);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(X =>
            {
                X.RequireHttpsMetadata      = false;
                X.SaveToken                 = true;
                X.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });
        }