首页 > 解决方案 > .Net Core 3.1 作为独立的身份服务器

问题描述

我想使用 .Net Core 3.1 Web 应用程序来允许应用程序(例如 iPhone 或 Web Javascript)使用用户名和密码进行身份验证并接收包含有关用户声明的 Jason Web 令牌(JWT)......如果它成功,然后 JWT 可以作为不记名令牌发送到 API,该 API 将解码和验证 JWT(令牌将是非对称的并使用公钥/私钥对)并检索嵌入的任何声明......也许是一个奖励如果应用程序也可以解码 JWT 以检索任何声明。

关于这种方法是否可行的任何想法?而且,如果有任何关于如何做到这一点的讨论或示例,那就太好了。

在此处输入图像描述

标签: .net-corejwtidentityserver4asp.net-core-3.1encryption-asymmetric

解决方案


查看 IdentityServer4 提供的示例。此示例/快速入门包括您所描述的案例。https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts/6_AspNetIdentity/src

API 需要是 IdentityServer4 配置中的一个范围。它与权限 (IdentityServer4) 有连接:

services.AddAuthentication("Bearer")
                .AddJwtBearer("Bearer", options =>
                {
                    options.Authority = "https://localhost:5001";
                    
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateAudience = false
                    };
                });

客户端,在本例中为 MVC 客户端,需要是 IdentityServer4 中的客户端。GrantType 有多种类型。https://identityserver4.readthedocs.io/en/latest/topics/grant_types.html

services.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority = "https://localhost:5001";

                options.ClientId = "mvc";
                options.ClientSecret = "secret";
                options.ResponseType = "code";
                
                options.Scope.Add("api1");

                options.SaveTokens = true;
            });

希望这可以帮助你


推荐阅读