# .NET SDK Configuration and Usage

This guide will walk you through the step-by-step process of configuring your LicenseSpring license using the LicenseSpring .NET SDK.

As you advance through this tutorial, you'll gain the expertise to activate, deactivate, and verify licenses directly from your .NET application.

Throughout this tutorial, functions and methods employed may throw exceptions to address potential issues.

For more information about these, see [**Error Handling**](https://docs.licensespring.com/sdks/tutorials/getting-started/.net-c/broken-reference).

### Configuration

To initialize the SDK, you must fill in your application name and version, LicenseSpring API key, shared key and product code.

Both the API key and shared key can be found on your LicenseSpring account under Settings->Keys:

![API and Shared Key Location](https://content.gitbook.com/content/gLzurdfXUuKr9IziZWLz/blobs/fNNEzjn5kLta5BiZQV4F/qp4PTqnTV2l2iS1sTmKn5_image.png)

Your product code is located in your product list under "Configure Products":

![Product Code Location](https://content.gitbook.com/content/gLzurdfXUuKr9IziZWLz/blobs/soHrb0fmAea5keIe473j/M4Ii6KreWKfvVkT9ACmr3_image.png)

Please, keep in mind that the product code field is case-sensitive.

When you have the necessary values, create the configuration as shown below:

```csharp
var configuration = new LicenseSpring.Configuration(
  apiKey: "API KEY",
  sharedKey: "SHARED KEY",
  productCode: "PRODUCT SHORT CODE",
  appName: "APP NAME",
  appVersion: "VERSION");
// Or use OAuth instead of API keys:
OAuthCredentials oAuth = new OAuthCredentials("CLIENT ID", "CLIENT SECRET");
// Don't forget to provide your CryptoProvider key if you will use the default crypto provider. If you will provide your own ICryptoProvider implementation you can skip this.
oAuth.CryptoProviderKey = "My secret crypto key.";
configuration = new LicenseSpring.Configuration(
  oAuthCredentials: oAuth,
  productCode: "PRODUCT SHORT CODE",
  appName: "APP NAME",
  appVersion: "VERSION");
```

Now you can initialize `LicenseManager` using this configuration object:

```csharp
var licenseManager = LicenseManager.GetInstance();
licenseManager.Initialize(configuration); 
```

### License Activation

The license keys are located in "Licenses" section on the vendor platform:

![License Key Location](https://content.gitbook.com/content/gLzurdfXUuKr9IziZWLz/blobs/ZxHaepHR9TQYkwouib9J/tkZUO%20Ut3up1SXzxNIj9R_image.png)

To implement user-based licensing please refer to: [**User-Based Licensing**](https://docs.licensespring.com/sdks/tutorials/getting-started/.net-c/broken-reference).

To activate a license, create a `LicenseID` using the given key and call activation method.

```csharp
try 
{
  LicenseID licenseId = LicenseID.FromKey( licenseKey );
  var license = licenseManager.ActivateLicense( licenseId );
} 
catch( LicenseActivationException e )
{
  //...
}
```

{% hint style="info" %}
**Note:** `ActivateLicense` returns the activated `License` object if the activation is successful.
{% endhint %}

{% hint style="danger" %}
`ActivateLicense` can throw many different exceptions, for information on why they're thrown and how to handle them, see [**Error Handling**](https://docs.licensespring.com/sdks/tutorials/getting-started/.net-c/broken-reference).
{% endhint %}

{% hint style="info" %}
**InvalidOperationException:** can occur due to incorrect use of the `ActivateLicense` function. This function has two overloads. For a regular license activation, it takes only `LicenseID`. The overload that takes strings is used for SSO. For more information, see [**Single Sign-On**](https://docs.licensespring.com/sdks/tutorials/getting-started/.net-c/broken-reference).
{% endhint %}

### License Deactivation

Deactivating a license unbinds the device from the license, and decrements the number of total activations.

Depending on the license transfer policy, this usually means that the license is freed up to be activated onto a new device.

Deactivating is done using the following method:

```csharp
var isDeactivated = license.Deactivate();
```

{% hint style="info" %}
**Note:** `Deactivate()` has an optional Boolean parameter that can be passed to determine whether the local license and folders created by the SDK should also be removed. This parameter is **false by default**.
{% endhint %}

{% hint style="info" %}
**Note:** `Deactivate()` returns true if successfully deactivated and false otherwise. It also returns true if the license is already inactive.
{% endhint %}

### Local License

The local license is a copy of the license information that is saved on the end user's local computer. Users who have not activated the license on their end before will not have a local license. By default, the local license is stored: `%USERPROFILE%\AppData\Local\LicenseSpring\”Product Code”\License.key`.

See [**Local License File**](https://docs.licensespring.com/sdks/tutorials/getting-started/.net-c/broken-reference) for more information.

If a device already has a license, and you try to activate another license on that device, for the same product, that newly activated license will override the existing license, deleting it from the device. This will be on the developer to make sure that they check if a license exists on the device before a new activation.

The current license can be retrieved with:

```csharp
var currentLicense = licenseManager.CurrentLicense();
```

{% hint style="info" %}
**Note:** If no active license is available, `currentLicense` will be set to `null`.
{% endhint %}

### License Check

It is recommended to perform a local license check at application start to confirm that the local license file belongs to the current device and has not been transferred.

A local license check is performed in the .NET SDK with:

```csharp
license.LocalCheck();
```

{% hint style="info" %}
**Note:** It is also useful to check whether the local license file has been tampered with and whether the local license is still valid.
{% endhint %}

Online license check refreshes the local license with the data from LicenseSpring backend are done with:

```javascript
var installationFile = license.Check();
```

This method also returns the most recent `InstallationFile` available for the license which can be useful for managing software updates.

For more information about managing software updates, see [**Handling Product Versions**](https://docs.licensespring.com/sdks/tutorials/getting-started/.net-c/broken-reference).

You can also check for active, valid, enabled, and expired licenses using `isActive`, `isValid`, `isEnabled`, and `isExpired` methods respectively.

Note that an active license does not necessarily mean a license is valid. A valid license means the license is active, enabled, and not expired. An active license means that the license is bound to a device, but may also be disabled or expired.

### Full Code Sample

Below you can find a code sample that initializes the SDK, retrieves a local license or activates a new one, and performs the necessary checks.

```csharp
var configuration = new LicenseSpring.Configuration(
  apiKey: "API KEY",
  sharedKey: "SHARED KEY",
  productCode: "PRODUCT SHORT CODE",
  appName: "APP NAME",
  appVersion: "VERSION");
  
try
{
  var licenseManager = LicenseManager.GetInstance();
  licenseManager.Initialize(configuration); 
  var currentLicense = licenseManager.CurrentLicense();
  if( currentLicense == null )
  {
     LicenseID licenseId = LicenseID.FromKey( licenseKey );
     currentLicense = licenseManager.ActivateLicense( licenseId );
  }
  currentLicense.LocalCheck();
  currentLicense.Check();
}
catch (const LicenseSpringException& ex )
{
  Console.WriteLine("Exception encountered");
}
```
