|
|
|
Wur.Dip.Brightspace.AuthSdk
|
|
|
|
======[](url)
|
|
|
|
**Wur.Dip.Brightspace.AuthSdk** is a nuget package for dealing with Brightspace authentication in .NET core.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
1. Voeg de nuget source toe voor dit package.
|
|
|
|
|
|
|
|
|
|
|
|
- Vraag een deploy token aan.
|
|
|
|
- Voeg een Nuget source toe aan het project. Dat kan in Visual Studio, met nuget.exe of door het plaatsen van een bestand met de naam _nuget.config_ in de root van de solution.
|
|
|
|
|
|
|
|
_nuget.config_ voorbeeld:
|
|
|
|
```
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<configuration>
|
|
|
|
<packageSources>
|
|
|
|
<add key="Wur.Dip.Brightspace.Authsdk" value="https://git.wur.nl/api/v4/projects/7425/packages/nuget/index.json" />
|
|
|
|
</packageSources>
|
|
|
|
<packageSourceCredentials>
|
|
|
|
<Wur.Dip.Brightspace.Authsdk>
|
|
|
|
<add key="Username" value="[deploy token gebruikersnaam]"/>
|
|
|
|
<add key="ClearTextPassword" value="[deploytoken]"/>
|
|
|
|
</Wur.Dip.Brightspace.Authsdk>
|
|
|
|
</packageSourceCredentials>
|
|
|
|
</configuration>
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
2. Het nuget package kan nu worden toegevoegd aan het project.
|
|
|
|
|
|
|
|
[]([url](url))
|
|
|
|
![File](doc/img/file.png)
|
|
|
|
![Select](doc/img/select.png)
|
|
|
|
|
|
|
|
## Using
|
|
|
|
|
|
|
|
Zonder dependency injection:
|
|
|
|
|
|
|
|
```
|
|
|
|
BrightspaceSettings settings = new BrightspaceSettings()
|
|
|
|
{
|
|
|
|
Host = "wurnldev.brightspace.com",
|
|
|
|
HostScheme = "https",
|
|
|
|
HostPort = 443,
|
|
|
|
AppId = "xxxxxxxxx",
|
|
|
|
AppKey = "xxxxxxxx",
|
|
|
|
UserId = "xxxxxx",
|
|
|
|
UserKey = "xxxxxx"
|
|
|
|
};
|
|
|
|
|
|
|
|
Authenticator authenticator = new Authenticator(settings);
|
|
|
|
var url = authenticator.CreateAuthenticatedUri("/d2l/api/versions/", HttpMethod.Get);
|
|
|
|
|
|
|
|
using var response = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
Console.WriteLine(json);
|
|
|
|
```
|
|
|
|
|
|
|
|
Een voorbeeld met dependency injection:
|
|
|
|
|
|
|
|
- appsettings.json
|
|
|
|
|
|
|
|
```
|
|
|
|
"Brightspace": {
|
|
|
|
"HostScheme": "https",
|
|
|
|
"HostPort": 443,
|
|
|
|
"Host": "wurnldev.brightspace.com",
|
|
|
|
"AppId": "xxxxxxx",
|
|
|
|
"AppKey": "xxxxxxx",
|
|
|
|
"UserId": "xxxxxx",
|
|
|
|
"UserKey": "xxxxx"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- Startup.cs
|
|
|
|
|
|
|
|
```
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
|
|
|
services.AddControllers();
|
|
|
|
|
|
|
|
services.AddBrightspaceConfiguration(Configuration);
|
|
|
|
services.AddHttpClient();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- HomeController.cs
|
|
|
|
|
|
|
|
```
|
|
|
|
[ApiController]
|
|
|
|
[Route("[controller]")]
|
|
|
|
public class HomeController : ControllerBase
|
|
|
|
{
|
|
|
|
|
|
|
|
private readonly HttpClient m_httpClient;
|
|
|
|
private readonly Authenticator m_authenticator;
|
|
|
|
|
|
|
|
public HomeController(IHttpClientFactory httpClient, IOptions<BrightspaceSettings> brightOptions)
|
|
|
|
{
|
|
|
|
m_httpClient = httpClient.CreateClient();
|
|
|
|
m_authenticator = new Authenticator(brightOptions.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<ActionResult<string>> Get()
|
|
|
|
{
|
|
|
|
var url = m_authenticator.CreateAuthenticatedUri("/d2l/api/versions/", HttpMethod.Get);
|
|
|
|
|
|
|
|
using var response = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
|
|
return Ok(json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|