ABP Framework Consuming HTTP APIs from a .NET Client

邪不压正!發表於2024-08-29
1、Add BookStore_App section int BookStore.DbMigrator\appsettings.json
"BookStore_App": {
  "ClientId": "BookStore_App",
  "ClientSecret": "1q2w3e*",
  "RootUrl": "https://localhost:44333"
}

2、Add Client in BookStore.Domain\OpenIddict\OpenIddictDataSeedContributor.cs
// BookStoreApp Client
var bookStoreAppClientId = configurationSection["BookStore_App:ClientId"];
if (!bookStoreAppClientId.IsNullOrWhiteSpace())
{
    var bookStoreAppRootUrl = configurationSection["BookStore_App:RootUrl"]?.TrimEnd('/');
    await CreateApplicationAsync(
        name: bookStoreAppClientId,
        type: OpenIddictConstants.ClientTypes.Confidential,
        consentType: OpenIddictConstants.ConsentTypes.Implicit,
        displayName: "BookStoreApp",
        scopes: commonScopes,
        grantTypes: [OpenIddictConstants.GrantTypes.Password,],
        secret: configurationSection["BookStore_App:ClientSecret"] ?? "1q2w3e*",
        redirectUri: $"{bookStoreAppRootUrl}/authentication/login-callback"
    );
}

3、Run DbMigrator project

4、Add ClientSecret to BookStore\test\BookStore.HttpApi.Client.ConsoleTestApp\appsettings.json
{
  "RemoteServices": {
    "Default": {
      "BaseUrl": "https://localhost:44333"
    }
  },
  "IdentityClients": {
    "Default": {
      "GrantType": "password",
      "ClientId": "BookStore_App",
      "UserName": "admin",
      "UserPassword": "1q2w3E*",
      "ClientSecret": "1q2w3e*",
      "Authority": "https://localhost:44333",
      "Scope": "BookStore"
    }
  }
}

5、Run API and test again

  1. Add BookStore_App section int BookStore.DbMigrator\appsettings.json

"BookStore_App": { "ClientId": "BookStore_App", "ClientSecret": "1q2w3e*", "RootUrl": "https://localhost:44333" }

  1. Add Client in BookStore.Domain\OpenIddict\OpenIddictDataSeedContributor.cs

// BookStoreApp Client var bookStoreAppClientId = configurationSection["BookStore_App:ClientId"]; if (!bookStoreAppClientId.IsNullOrWhiteSpace()) { var bookStoreAppRootUrl = configurationSection["BookStore_App:RootUrl"]?.TrimEnd('/'); await CreateApplicationAsync( name: bookStoreAppClientId, type: OpenIddictConstants.ClientTypes.Confidential, consentType: OpenIddictConstants.ConsentTypes.Implicit, displayName: "BookStoreApp", scopes: commonScopes, grantTypes: [ OpenIddictConstants.GrantTypes.Password, ], secret: configurationSection["BookStore_App:ClientSecret"] ?? "1q2w3e*", redirectUri: $"{bookStoreAppRootUrl}/authentication/login-callback" ); }

  1. Run DbMigrator project

  2. Add ClientSecret to BookStore\test\BookStore.HttpApi.Client.ConsoleTestApp\appsettings.json

{ "RemoteServices": { "Default": { "BaseUrl": "https://localhost:44333" } }, "IdentityClients": { "Default": { "GrantType": "password", "ClientId": "BookStore_App", "UserName": "admin", "UserPassword": "1q2w3E*", "ClientSecret": "1q2w3e*", "Authority": "https://localhost:44333", "Scope": "BookStore" } } }

5 Run API and test again

相關文章