用于与 Okta 管理 API 交互的 .NET SDK,可让服务器端代码管理 Okta 用户、组、应用程序等。
[][devforum] [][dotnetdocs]
This repository contains the Okta management SDK for .NET. This SDK can be used in your server-side code to interact with the Okta management API and:
Note: For more details about the APIs and models the SDK support, check out the API docs
We also publish these other libraries for .NET:
You can learn more on the [Okta + .NET][lang-landing] page in our documentation.
This library uses semantic versioning and follows Okta's library version policy.
:heavy_check_mark: The current stable major version series is: 10.x
| Version | Status |
|---|---|
| 10.x | :heavy_check_mark: Stable (migration guide |
| 9.x | :warning: Retiring on April 3rd 2026 |
| 8.x | :x: Retired |
The latest release can always be found on the [releases page][github-releases]. For more information about our SDKs' lifecycle, check out our docs.
If you run into problems using the SDK, you can
The SDK is compatible with:
Visual Studio 2017 or newer is required as previous versions are not compatible with the above frameworks.
Okta.Sdk package.Simply run install-package Okta.Sdk. Done!
You'll also need:
Construct a client instance by creating a Configuration object, setting your Okta domain name and API token and passing the Configuration object to the API client class constructor:
…Hard-coding the Okta domain and API token works for quick tests, but for real projects you should use a more secure way of storing these values (such as environment variables). This library supports a few different configuration sources, covered in the configuration reference section.
To use the API client with an HTTP proxy, you can either setup your proxy via different configuration sources, covered in the configuration reference section, or via API constructor. If you have both, the proxy passed via constructor will take precedence.
System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
var appsApi = new ApplicationApi(webProxy : webProxy);Beginning with Okta.Sdk version 9.2.0 you can use the OktaApiClientOptions class to instantiate API clients using a fluent builder syntax. The static methods defined on OktaApiClientOptions return an instance of OktaApiServiceBuilder which provides a mechanism to specify more complex configuration options.
…Using OktaApiClientOptions and the resulting OktaApiServiceBuilder you can instantiate multiple API clients that have the same configuration:
…Beginning with Okta.Sdk version 9.2.0, using OktaApiServiceBuilder by way of OktaApiClientOptions, you can specify custom Interceptors to be used during API client operations. Further, if your custom implementation has constructor dependencies, those dependencies should be specified using the For and Use methods defined on OktaApiServiceBuilder.
…See also:
Beginning with Okta.Sdk version 9.2.0, using OktaApiServiceBuilder by way of OktaApiClientOptions, you can specify custom HttpMessageHandler to be used during API client operations. Further, if your custom implementation has constructor dependencies, those dependencies should be specified using the For and Use methods defined on OktaApiServiceBuilder.
…Okta allows you to interact with Okta APIs using scoped OAuth 2.0 access tokens. Each access token enables the bearer to perform specific actions on specific Okta endpoints, with that ability controlled by which scopes the access token contains.
This SDK supports this feature only for service-to-service applications. Check out our guides to learn more about how to register a new service application using a private and public key pair.
When using this approach you won't need an API Token because the SDK will request an access token for you. In order to use OAuth 2.0, construct an API client instance by passing the following parameters:
var oauthAppsApi = new ApplicationApi(new Configuration
{
OktaDomain = "https://{{yourOktaDomain}}",
AuthorizationMode = AuthorizationMode.PrivateKey,
ClientId = "{{clientId}}",
Scopes = new List { "okta.users.read", "okta.apps.read" }, // Add all the scopes you need
PrivateKey = new JsonWebKeyConfiguration(jsonString)
});Key object for assigning to the PrivateKey can be created and initialized inline like in this example for RSA key:
var privateKey = new JsonWebKeyConfiguration
{
P = "{{P}}",
Kty = "RSA",
Q = "{{Q}}",
D = "{{D}}",
E = "{{E}}",
Kid = "{{P}}",
Qi = "{{Qi}}"
};
var configuration = new Configuration
{
OktaDomain = "https://{{yourOktaDomain}}",
AuthorizationMode = AuthorizationMode.PrivateKey,
ClientId = "{{clientId}}",
Scopes = new HashSet { "okta.users.read", "okta.apps.read" }, // Add all the scopes you need
PrivateKey = privateKey
};
var oauthAppsApi = new ApplicationApi(configuration);Both examples above hand the SDK the key material itself, which means it has to exist somewhere readable - in okta.yaml, in appsettings.json, or in a string in memory. If the key lives somewhere that signs on your behalf without surrendering it, such as a TPM, a hardware security module, or a non-exportable certificate, set PrivateKeySigningCredentials instead of PrivateKey and the SDK signs the client assertion through it:
var cngKey = CngKey.Open("my-okta-key", new CngProvider("Microsoft Platform Crypto Provider"));
var configuration = new Configuration
{
OktaDomain = "https://{{yourOktaDomain}}",
AuthorizationMode = AuthorizationMode.PrivateKey,
ClientId = "{{clientId}}",
Scopes = new HashSet { "okta.users.read" },
PrivateKeySigningCredentials =
new SigningCredentials(new RsaSecurityKey(new RSACng(cngKey)), SecurityAlgorithms.RsaSha256),
};Any SigningCredentials works, so the same applies to an X509Certificate2 whose key cannot be exported or to a key held in a cloud key store. PrivateKeySigningCredentials takes precedence over PrivateKey when both are set, and it is deliberately never read from or written to a configuration file.
It is possible to use an access token you retrieved outside of the SDK for authentication. For that, set Configuration.AuthorizationMode configuration property to AuthorizationMode.BearerToken and Configuration.AccessToken to the token string.
Note: Starting from 8.x series the Okta management SDK added support for DPoP. If the SDK detects the application has DPoP enabled, it will silently proceed to obtain a DPoP-bound access token, and will generate a new DPoP Proof JWT for every request. There's no additional configuration required for developers.
These examples will help you understand how to use this library. You can also browse the full [API reference documentation][dotnetdocs].
Once you initialize an API client, you can call methods to make requests to the Okta API.
// Get the user with a user ID or login
var user = await userApi.GetUserAsync("");The string argument for GetUserAsync can be the user's ID or the user's login (usually their email).
The SDK will automatically paginate Okta collections for you:
// These different styles all perform the same action:
var allUsers = await userApi.ListUsers().ToListAsync();
var allUsers = await userApi.ListUsers().ToArrayAsync();var foundUsers = await userApi
.ListUsers(search: $"profile.nickName eq \"Skywalker\"")
.ToArrayAsync();…// Activate the user
var userLifecycleApi = new UserLifecycleApi();
await userLifecycleApi.ActivateUserAsync(createdUser.Id, false);// Update profile
createdUser.Profile.NickName = nickName;
var updateUserRequest = new UpdateUserRequest
{
Profile = createdUser.Profile
};
var updatedUser = await _userApi.UpdateUserAsync(createdUser.Id, updateUserRequ暂无开放 Issues,或尚未同步最近议题。