我有一个 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 });
}