使用Identity Server 4的ASP.NET Web Form客户端

15

我有一个包含以下内容的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 Form客户端的样例。您能否建议如何使用ASP.NET身份验证对Web Form用户进行身份验证,然后使用访问令牌调用API?

我没有看到与Web Form客户端示例相配合的Identity Server 4样例。

Identity Server 3有一个示例,但它在startup中完成了所有操作。

当我查看Identity Server 4的MVC客户端时,它在configure方法中具有所有设置,然后像这样调用。

我该如何在Web Form中应用Authorize属性,以便在调用API时被重定向到Identity Server 4进行登录,然后在登录后调用API?

如何为Web Form更改客户端?

 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 });
                }

你找到解决方案了吗? - user7730840
1个回答

3

虽然晚了,但希望能对某些人有所帮助,Web表单仍然得到支持。
使用Startup与Web表单没有问题。唯一的限制是不允许在那里使用AuthorizeAttribute,但这并不是问题,只需放置:

app.UseStageMarker(PipelineStage.Authenticate);

在您的底部
public void Configuration(IAppBuilder app)

OWIN启动程序中的方法。

可以从我的github获取示例Startup实现。它适用于MVC、Web Forms,并且还从IdentityServer v.3'代码库中带来了JWT验证,已升级以与最新的OWIN库编译。


如果我仍然有任何不清楚的地方,请在评论中不要犹豫询问。


太完美了!真希望几天前就看到这个了。 - d3n13d1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接