Access Token

Overview

When using an OpenID Connect (OIDC) authentication method with WorkflowGen, an OAuth 2.0 access token is available for server-side .NET development. This token enables you to make requests to the GraphQL API as well as to your own APIs, depending on your provider's configuration.
For instructions on how to configure an OIDC provider with WorkflowGen, see the WorkflowGen for Azure for Azure Active Directory or the WorkflowGen Technical Guide for Active Directory Federation Services (AD FS), Auth0, and Okta.

Using an access token in web form code-behind

You can get the current user's access token from the new this.CurrentUserAccessToken() public instance method of the WorkflowPage class (available since WorkflowGen.My version 4.6.0) in order to make calls from the code-behind to WorkflowGen's GraphQL API or to your own APIs. To do this, follow the example code below:
1
protected void Page_Load(object sender, EventArgs e)
2
{
3
base.Page_Load(sender, e);
4
5
var accessToken = this.CurrentUserAccessToken();
6
7
var query = @"
8
query {
9
viewer {
10
firstName
11
lastName
12
userName
13
}
14
}
15
";
16
17
using (var client = new System.Net.Http.HttpClient
18
{
19
BaseAddress = new System.Uri("http://localhost/wfgen/graphql")
20
})
21
{
22
client.DefaultRequestHeaders.Accept.Clear();
23
client.DefaultRequestHeaders.Accept.Add(
24
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
25
);
26
client.DefaultRequestHeaders.Authorization =
27
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
28
29
var jsonQuery = Newtonsoft.Json.JsonConvert.SerializeObject(new
30
{
31
query = query,
32
operationName = "",
33
variables = new {}
34
});
35
var response = client.PostAsync(string.Empty, new System.Net.Http.StringContent(jsonQuery, System.Text.Encoding.UTF8, "application/json")).Result;
36
37
response.EnsureSuccessStatusCode();
38
39
// Display the result in a web form field for debug purposes
40
REQUEST_DESCRIPTION.Text = response.Content.ReadAsStringAsync().Result;
41
}
42
}
Copied!
There are settings that need to be added to the WebForms web.config file in order for this example to work. You must add the reference to the System.Net.Http namespace and deactivate unobtrusive validation mode. Here is a minimal web.config content that you can use:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<configuration>
3
<appSettings>
4
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
5
</appSettings>
6
<system.web>
7
<compilation debug="false" targetFramework="4.6.1">
8
<assemblies>
9
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
10
</assemblies>
11
</compilation>
12
<httpRuntime targetFramework="4.6.1" />
13
</system.web>
14
</configuration>
Copied!
Last modified 1yr ago