具有身份服务器 4 的 asp.net Web 表单客户端

2023-12-26

我有一个 asp.net 解决方案,其中包括

1). asp.net identity server rc 3
2). asp.net Core web api
3). asp.net webform ( not in asp.net core, client)

我没有看到任何带有 Identity Server 4 和 Web 表单客户端的示例。您能否建议如何使用具有 asp.net 身份的身份服务器对 Web 表单用户进行身份验证,然后使用访问令牌调用 api?

我没有看到 Identity Server 4 示例网络表单客户端 https://github.com/IdentityServer/IdentityServer4.Samples/tree/dev/Clients/src or sample https://github.com/IdentityServer/IdentityServer4.Samples/tree/dev/Quickstarts

身份服务器3有一个sample https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source/Clients/WebFormsClient但它正在做一切startup https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/Clients/WebFormsClient/Startup.cs

当我看见MVC客户端 https://github.com/IdentityServer/IdentityServer4.Samples/blob/dev/Quickstarts/6_AspNetIdentity/src/MvcClient/Startup.cs对于身份服务器4,它在配置方法中具有所有设置,然后像这样调用它this https://github.com/IdentityServer/IdentityServer4.Samples/blob/dev/Quickstarts/6_AspNetIdentity/src/MvcClient/Controllers/HomeController.cs

我将如何在 webform 中应用 Authorize 属性,以便我重定向到身份服务器 4 进行登录,然后在登录后当我像这样调用 api 时:

如何更改网络表单的客户端?

 new Client()
                  {
                    ClientId = "mvcClient",
                    ClientName = "MVC Client",                    
                    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

                    ClientSecrets = new List<Secret>()
                    {
                        new Secret("secret".Sha256())
                    },

                    RequireConsent = false;

                    // where to redirect to after login
                    RedirectUris = { "http://localhost:5002/signin-oidc" },
                    // where to redirect to after logout
                    PostLogoutRedirectUris = { "http://localhost:5002" },

                    AllowedScopes =
                    {
                        StandardScopes.OpenId.Name,
                        StandardScopes.Profile.Name,
                        StandardScopes.OfflineAccess.Name,
                        StandardScopes.Roles.Name,
                        "API"
                    }
                }

new InMemoryUser()
            {
                Subject = "1",
                Username = "testuser",
                Password = "password",
                Claims = new List<Claim>()
                {
                    new Claim("name", "Alice"),
                    new Claim("Website", "http://alice.com"),
                     new Claim(JwtClaimTypes.Role, "admin")

                }
            }


 return new List<Scope>()
                {
                    StandardScopes.OpenId, // subject id
                    StandardScopes.Profile, // first name, last name
                    StandardScopes.OfflineAccess, 
                   StandardScopes.Roles,
                    new Scope()
                    {
                        Name = "API",
                        Description = "API desc",
                         Type = ScopeType.Resource,
                        Emphasize = true,
                        IncludeAllClaimsForUser = true,
                        Claims = new List<ScopeClaim>
                        {
                            new ScopeClaim(ClaimTypes.Name),      
                            new ScopeClaim(ClaimTypes.Role)
                        }
                    }
                };


 public void CallApiUsingClientCredentials()
                {
                    var tokenClient = new TokenClient("http://localhost:5000/connect/token", "mvc", "secret");
                    var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");

                    var client = new HttpClient();
                    client.SetBearerToken(tokenResponse.AccessToken);
                    var content = await client.GetStringAsync("http://localhost:5001/identity");

                    var result = JArray.Parse(content).ToString();

                }

 [Authorize(Roles="admin)]
          [HttpGet]
           public IActionResult Get()
                    {
                        return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
                }

迟到的答案,但希望它能帮助某人,仍然支持网络表单。
启动与web表单一起使用是没有问题的。唯一的限制是没有地方AuthorizeAttribute在那里,但这仍然不是问题,只需输入:

app.UseStageMarker(PipelineStage.Authenticate);

在你的底部

public void Configuration(IAppBuilder app)

OWIN Startup 中的方法。

启动实施示例可以从我的github上获取 https://github.com/dfrunet/SampleClients/tree/master/OidcClientHelper。它与 MVC、Web 表单配合使用,还从 IdentityServer v.3 的代码库中引入了 JWT 验证,并升级为使用最新的 OWIN 库进行编译。


如果我还有什么不清楚的地方,请随时在评论中提问。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有身份服务器 4 的 asp.net Web 表单客户端 的相关文章

随机推荐