• AspNetCore OpenId


    1 Server端

        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddIdentityServer()
                    .AddDeveloperSigningCredential()
                    .AddInMemoryClients(Config.GetClients())
                    .AddInMemoryApiResources(Config.GetResource())
                    .AddInMemoryIdentityResources(Config.GetIdentityResource())
                    .AddTestUsers(Config.GetUsers());
    
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                app.UseIdentityServer();
                app.UseMvcWithDefaultRoute();
            }
        }
    
        public class Config
        {
            public static List<ApiResource> GetResource()
            {
                return new List<ApiResource>
                {
                    new ApiResource("api1","Api Application "),
                };
            }
            public static List<IdentityResource> GetIdentityResource()
            {
                return new List<IdentityResource>
                {
                    new  IdentityResources.OpenId(),
                    new IdentityResources.Profile(),
                    new IdentityResources.Email(),
                };
            }
            public static List<Client> GetClients()
            {
                return new List<Client>
                {
                    //客户端模式
                    //new Client{
                    //    ClientId="client",
                    //    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    //    ClientSecrets = {
                    //        new Secret("secret".Sha256())
                    //    },
                    //    AllowedScopes={ "api"},
                    //     },
    
                    ////密码模式
                    //  new Client{
                    //    ClientId="pwdclient",
                    //    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    //    ClientSecrets = {
                    //        new Secret("secret".Sha256())
                    //    },
                    //    AllowedScopes={ "api"},
                    //     },
    
                      //隐式模式
                         new Client{
                        ClientId="mvc",
                        AllowedGrantTypes = GrantTypes.Implicit,
                        ClientSecrets = {
                            new Secret("secret".Sha256())
                        },
                        //是否需要用户点击按钮
                        RequireConsent=false,
                        RedirectUris={ "http://localhost:5003/signin-oidc"},
                        PostLogoutRedirectUris={ "http://localhost:5003/signout-callback-oidc"},
                        AllowedScopes={
                                 IdentityServerConstants.StandardScopes.Profile,
                                 IdentityServerConstants.StandardScopes.OpenId,
                             },
                         },
                };
            }
    
    
            public static List<TestUser> GetUsers()
            {
                return new List<TestUser>
                {
                     new TestUser{SubjectId="10000",Username="yan",Password="123123" },
                };
            }
        }
    

      2 客户端

    public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddAuthentication(option =>
                {
                    option.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    option.DefaultChallengeScheme = "oidc";
                })
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddOpenIdConnect("oidc", options =>
                {
                    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.Authority = "http://localhost:5000";
                    options.RequireHttpsMetadata = false;
                    options.ClientId = "mvc";
                    options.ClientSecret = "secret";
                    options.SaveTokens = true;
                });
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
                app.UseStaticFiles();
                app.UseAuthentication();
                app.UseMvcWithDefaultRoute();
            }
        }
    

      3 客户端加Authorize标记

  • 相关阅读:
    【BUG】android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi/toolbar_line.png from
    关于 折半查找 while 条件 &lt; , &lt;=
    Unity3D——加入剑痕效果(PocketRPG Trail插件)
    用外部物理路由器时使用Neutron dhcp-agent提供的metadata服务(by quqi99)
    项目经理之项目经理注意事项
    让你提前认识软件开发(37):研发流程初探
    1.RunLoop是什么?
    列表类型内置方法
    字符串类型内置方法
    数字类型内置方法
  • 原文地址:https://www.cnblogs.com/a121984376/p/10031890.html
Copyright © 2020-2023  润新知