【问题标题】:asp.net web form client with identity server 4带有身份服务器 4 的 asp.net Web 表单客户端
【发布时间】:2017-04-09 03:30: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)

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

我没有看到带有 web form clientsample 的身份服务器 4 示例

身份服务器 3 有一个 sample,但它在 startup 中做所有事情

当我看到身份服务器 4 的 mvc client 时,它具有配置方法中的所有设置,然后像 this 一样调用它

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

如何更改 webform 的客户端?

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

【问题讨论】:

  • 找到解决方案了吗?

标签: webforms asp.net-core asp.net-identity identityserver3 identityserver4


【解决方案1】:

迟到的答案,但希望它对仍然支持网络表单的人有所帮助。
将启动与 Web 表单一起使用是没有问题的。唯一的限制是AuthorizeAttribute 没有地方,不过还是没问题,放:

app.UseStageMarker(PipelineStage.Authenticate);

在你的底部

public void Configuration(IAppBuilder app)

OWIN Startup 中的方法。

一个示例 Startup 实现 could be fetched from my github。它适用于 MVC、Web 表单,另外还带来了来自 IdentityServer v.3 代码库的 JWT 验证,升级后可以使用最新的 OWIN 库进行编译。


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

【讨论】:

  • 完美!希望我能在几天前​​遇到这个。
猜你喜欢
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
相关资源
最近更新 更多