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

问题描述 投票:12回答:2

我有一个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做的一切

当我看到mvc client用于身份服务器4时,它在configure方法中具有所有设置,然后像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
2个回答
0
投票

在WebForms中,您可以在web.config中设置授权

<configuration>
  <system.web>
    <authorization>
      <allow roles="domainname\Managers" />
      <deny users="*" />
    </authorization>
  </system.web>
</configuration>

来自answer on StackOverflow

另请参阅IdentityServer3示例中的web.config

  <location path="About">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>

0
投票

迟到的答案,但希望它可以帮助某人,仍然支持Web表单。 将启动与Web表单一起使用没有问题。唯一的限制是那里没有AuthorizeAttribute的地方,但它仍然没有问题,只是把:

app.UseStageMarker(PipelineStage.Authenticate);

在你的底部

public void Configuration(IAppBuilder app)

OWIN Startup中的方法。 一个示例启动实现could be fetched from my github。它适用于MVC,Web窗体,还可以从IdentityServer v.3'代码库中进行JWT验证,升级到使用最新的OWIN库进行编译。

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

© www.soinside.com 2019 - 2024. All rights reserved.