我正在使用我的身份服务器设置客户端凭据流,以从客户端获取访问令牌。我可以使用以下代码获取访问令牌,
身份服务器配置:
public void Configuration(IAppBuilder app) { app.Map("/identity", idsrvApp => { var corsPolicyService = new DefaultCorsPolicyService() { AllowAll = true }; var idServerServiceFactory = new IdentityServerServiceFactory() .UseInMemoryClients(Clients.Get()) .UseInMemoryScopes(Scopes.Get()) .UseInMemoryUsers(Users.Get()); var options = new IdentityServerOptions { Factory = idServerServiceFactory, SiteName = "Demo", IssuerUri = IdentityConstants.IssuerUri, PublicOrigin = IdentityConstants.STSOrigin, SigningCertificate = LoadCertificate() }; idsrvApp.UseIdentityServer(options); }); }
身份服务器 - 客户端配置:
public static class Clients { public static IEnumerable<Client> Get() { return new[] { new Client { ClientId = "ClientSDK", ClientName = "Client SDK (Client Credentials)", Flow = Flows.ClientCredentials, AllowAccessToAllScopes = true, ClientSecrets = new List<Secret>() { new Secret(IdentityConstants.ClientSecret.Sha256()) } } }; }
}
MVC 客户端:
var oAuth2Client = new TokenClient( IdentityConstants.STSTokenEndpoint, "ClientSDK", IdentityConstants.ClientSecret); var tokenResponse = oAuth2Client.RequestClientCredentialsAsync("MyScope").Result; return tokenResponse.AccessToken;
我能够获得访问令牌(即 JWT)。在创建令牌时使用其声明数据创建 JWT 时,请告诉我如何从我的数据库中添加一个唯一键,例如 (UserId)。