Sample App - danmarksmiljoeportal/rotter GitHub Wiki
1. Setup
Sample app: https://github.com/danmarksmiljoeportal/rotter/blob/main/Sample.zip
The sample app does a full authorization flow (OIDC/OAuth2) and then makes a request to ROTTE API (Test). Please contact Danmarks Miljøportal's support at [email protected] to get User Accounts with necessary claim types and values for testing the full login process.
How to generate C# API Client from swagger via NSwagStudio
With NSwag, you don't need an existing API—you can use third-party APIs that incorporate Swagger and generate a client implementation. NSwag allows you to expedite the development cycle and easily adapt to API changes.
- Download NSwagStudio and document at here: https://github.com/RicoSuter/NSwag/wiki/NSwagStudio
- After installing, use the this url for Specification URL
https://rotte.test.miljoeportal.dk/api/swagger/v1/swagger.json. - If there is any issue with
Required = Newtonsoft.Json.Required.DisallowNullwhen trying to generate the code (please refer to https://github.com/RicoSuter/NSwag/issues/1991).
2. OAuth authorization
The image below describes how the sample app requests authorization code and access token.

-
Sample app opens a browser tab with the authorization request (https://log-in.test.miljoeportal.dk/runtime/oauth2).
-
Authorization endpoint receives the authorization request, authenticates the user, and obtains authorization. Authenticating the user may involve chaining to other authentication systems.
-
Authorization server issues an authorization code to the redirect URI (https://localhost:7279).
-
Client receives the authorization code from the redirect URI.
-
Client app presents the authorization code at the token endpoint.
-
Token endpoint validates the authorization code and issues the tokens requested.
private static OidcClient InitializeLoginClient() { int port = 7279; string authority = "https://log-in.test.miljoeportal.dk/runtime/oauth2"; string clientId = "dmp-rotte-udv"; string redirectUri = string.Format($"https://localhost:{port}"); ....... ........ } -
CVR are required by Rotte Api
The login user must have valid CVR to access Jord api methods:
In the case, Rotte Api return Forbidden (403), just contact DMP to get correct CVR for the user.
3. Rotte Api Client
The RotteClient code is generated based on the config.nswag by using NSwagStudio. The client code exposes an easy way to get started consuming API's via a provided end point.
How to initialize a RotteClient:
var apiUrl = "https://rotte.test.miljoeportal.dk/api";
var rotteClient = await RestFactory.CreateAsync(apiUrl);
public static async Task<RotteClient> CreateAsync(string apiUrl)
{
var oidcClient = InitializeLoginClient();
Console.WriteLine("Logging in");
var loginResult = await Login(oidcClient);
// Request a new access token using the refresh token if it is expired.
//var refreshResult = await oidcClient.RefreshTokenAsync(loginResult.RefreshToken);
//Console.WriteLine(refreshResult.IdentityToken);
var httpClient = new HttpClient();
httpClient.SetBearerToken(loginResult.AccessToken);
Console.WriteLine("Initializing Rotte client");
var rotteClient = new RotteClient(apiUrl, httpClient);
return rotteClient;
}
After loggin in with the oidcClient (we've already set this up), httpClient will consume the AccessToken returned after the authorization as a bearer token to create a RotteClient. After creating the RotteClient successfully, you can use all the methods the client exposes itself.