C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
MachineToMachineAuth.cs
Go to the documentation of this file.
1using System;
2using System.Net.Http;
3using System.Threading.Tasks;
4using IdentityModel.Client;
5
7{
12 {
16 public BearerAuthenticationToken AccessToken { get; private set; }
17
21 public string ErrorDescription { get; private set; }
22
23 private readonly string _clientId;
24
25 private readonly string _secret;
26
27 private readonly string _authEndpoint;
28
29 private readonly string _scope;
30
38 public MachineToMachineAuth(string clientId, string secret, string scope, string authEndpoint)
39 {
40 _clientId = clientId;
41 _secret = secret;
42 _scope = scope;
43 _authEndpoint = authEndpoint;
44 }
45
49 public async Task<bool> Authorize()
50 {
51 var client = new HttpClient();
52 var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
53 {
54 Address = _authEndpoint,
55 ClientId = _clientId,
56 ClientSecret = _secret,
57 Scope = _scope
58 });
59
60 if (String.IsNullOrWhiteSpace(response.AccessToken))
61 {
62 ErrorDescription = response.ErrorDescription;
63 return false;
64 }
65
66 AccessToken = new BearerAuthenticationToken(response.AccessToken, DateTime.UtcNow);
67 return true;
68 }
69 }
70}
An AccessToken storing Bearer authentication information for requests made to the server,...
Machine-to-Machene authorization workflow.
BearerAuthenticationToken AccessToken
If Authorize() returns true then this property contains a new access token.
async Task< bool > Authorize()
Sets AccessToken and returns true if authorization was successful. Otherwise, it sets ErrorDescriptio...
MachineToMachineAuth(string clientId, string secret, string scope, string authEndpoint)
Machine-to-machine authorization workflow.
string ErrorDescription
If Authorize() returns false then this property contains an error message.