惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
D
Docker
J
Java Code Geeks
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
腾讯CDC
罗磊的独立博客
U
Unit 42
爱范儿
爱范儿
Vercel News
Vercel News
MyScale Blog
MyScale Blog

博客园 - 冰之玄岩,小小Programmer

程序员效率工具:3秒搞定JSON格式化,这个在线工具真香! 基于grafana+telegraf的服务器监控方案 Sql Server免域,异地备份 TeamCity+Docker k8s 安装步骤 Gitlab使用时的一些注意事项 Docker常用命令 AspNetCore中的IdentityServer4客户端认证模式实现 AspNet Core 认证 基于TeamCity的asp.net mvc/core,Vue 持续集成与自动部署 使用TFS 自动编译时的一点设置 SQL Server Update 语句使用Nolock 语法 SQl server 关于重复插入数据的测试 SQL Server 大数据量分页建议方案 页面轮换,ViewFlipper 和 ViewPager 的区别 Mono for android 如何动态添加View,线程内部如何更新UI. C#开发Android环境搭建 SPQuery.ViewAttributes (.Net 3.5Sp1)WebForm使用System.Web.Routing
AspNetCore OpenId
冰之玄岩,小小Programmer · 2018-11-28 · via 博客园 - 冰之玄岩,小小Programmer

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标记