Esempio n. 1
0
        private void ConfigureSwagger(IFunctionsHostBuilder Builder)
        {
            Builder.AddSwashBuckle(Assembly.GetExecutingAssembly(), (SwaggerDocOptions Options) => {
                Options.SpecVersion      = OpenApiSpecVersion.OpenApi3_0;
                Options.AddCodeParameter = false;
                Options.PrependOperationWithRoutePrefix = true;
                Options.XmlPath = "SkillsGardenApi.xml";

                Options.Documents = new[] {
                    new SwaggerDocument {
                        Name        = "v1",
                        Title       = "Skills Garden API",
                        Description = "Skills Garden API",
                        Version     = "v1"
                    }
                };

                Options.Title = "Swagger Specification";
                Options.ConfigureSwaggerGen = (SwaggerGenOptions Options) => {
                    Options.IncludeXmlComments(GetLocalFilename("SkillsGardenDTO.xml"));

                    Options.OperationFilter <SecurityRequirementsOperationFilter>();
                    Options.SchemaFilter <SwaggerRequiredFilter>();
                    Options.OperationFilter <SwaggerFormDataFilter>();

                    Options.CustomOperationIds((ApiDescription ApiDesc) => {
                        MethodInfo MethodInfo;
                        if (ApiDesc.TryGetMethodInfo(out MethodInfo))
                        {
                            return(MethodInfo.Name);
                        }
                        else
                        {
                            return(new Guid().ToString());
                        }
                    });

                    OpenApiSecurityScheme SecurityScheme = new OpenApiSecurityScheme();
                    SecurityScheme.Type         = SecuritySchemeType.Http;
                    SecurityScheme.Scheme       = "bearer";
                    SecurityScheme.Description  = "JWT for authorization";
                    SecurityScheme.BearerFormat = "JWT";
                    Options.AddSecurityDefinition("BearerAuth", SecurityScheme);
                };
            });
        }
Esempio n. 2
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
                };
            });
        }