Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            InMemoryConfiguration.Configuration = this.Configuration;

            services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseSuccessEvents     = true;
            })
            .AddDeveloperSigningCredential()
            //生产环境时需要使用AddSigningCredential()
            .AddInMemoryApiResources(InMemoryConfiguration.GetApiResources())
            .AddInMemoryIdentityResources(InMemoryConfiguration.GetIdentityResources())
            .AddInMemoryClients(InMemoryConfiguration.GetClients())
            .AddTestUsers(InMemoryConfiguration.GetUsers().ToList());

            // add CORS policy for non-IdentityServer endpoints
            services.AddCors(options =>
            {
                options.AddPolicy("api", policy =>
                {
                    policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
                });
            });
        }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(options =>
            {
                options.Filters.Add <TokenFilter>();                                    //����Զ���Ĺ�����
            });
            services.AddIdentityServer()                                                //Ids4����
            .AddDeveloperSigningCredential()                                            //��ӿ�����Աǩ��ƾ��
            .AddTestUsers(InMemoryConfiguration.Users().ToList())
            .AddInMemoryIdentityResources(InMemoryConfiguration.GetIdentityResources()) //����ڴ�apiresource
            .AddInMemoryApiResources(InMemoryConfiguration.GetApiResources())
            .AddInMemoryApiScopes(InMemoryConfiguration.GetApiScopes())
            .AddInMemoryClients(InMemoryConfiguration.GetClients());                              //�������ļ���Client������Դ�ŵ��ڴ�

            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) //�����֤
            .AddJwtBearer(options =>
            {
                options.Authority = "https://*****:*****@qq.com"
                    }
                });
                var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                var xmx      = Path.Combine(basePath, "IdentityService.xml");
                c.IncludeXmlComments(xmx);    //添加swagger注释
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "Please enter into field the word 'Bearer' followed by a space and the JWT value",
                    Name        = "Authorization",          //http请求的
                    In          = ParameterLocation.Header, //标记从哪个地方传入验证
                    Type        = SecuritySchemeType.ApiKey //加密类型
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { new OpenApiSecurityScheme
                      {
                          Reference = new OpenApiReference()
                          {
                              Id   = "Bearer",
                              Type = ReferenceType.SecurityScheme//传入类型
                          }
                      }, Array.Empty <string>() }
                });
            });
        }
Esempio n. 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // IoC - Service & Repository
            services.AddScoped <ILoginUserService, LoginUserService>();
            services.AddScoped <ILoginUserRepository, LoginUserRepository>();

            // IdentityServer4
            //string basePath = "Path to certifiates";

            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            //.AddSigningCredential(new X509Certificate2(Path.Combine(basePath,
            //    Configuration["Certificates:CerPath"]),
            //    Configuration["Certificates:Password"]))
            //.AddTestUsers(InMemoryConfiguration.GetUsers().ToList())
            .AddInMemoryClients(InMemoryConfiguration.GetClients())
            .AddInMemoryApiResources(InMemoryConfiguration.GetApiResources())
            .AddResourceOwnerValidator <ResourceOwnerPasswordValidator>()
            .AddProfileService <ProfileService>();
        }