Example #1
0
        public static void Main(string[] args)
        {
            // Relative or absolute path to the *.p12-file containing the test certificate
            var p12Filename = Environment.GetEnvironmentVariable("P12FILENAME");

            // Password required to use the certificate
            var p12Password = Environment.GetEnvironmentVariable("P12PWD");

            // The issuer as defined in Maskinporten
            var issuer = Environment.GetEnvironmentVariable("MASKINPORTEN_ISSUER");

            var cert          = new X509Certificate2(p12Filename, p12Password);
            var configuration = new MaskinportenClientConfiguration(
                audience: @"https://ver2.maskinporten.no/",           // ID-porten audience path
                tokenEndpoint: @"https://ver2.maskinporten.no/token", // ID-porten token path
                issuer: issuer,                                       // Issuer name
                numberOfSecondsLeftBeforeExpire: 10,                  // The token will be refreshed 10 seconds before it expires
                certificate: cert);
            var maskinportenClient = new MaskinportenClient(configuration);

            var tokenTask = maskinportenClient.GetAccessToken("ks:fiks").ContinueWith(t =>
            {
                var token = t.Result;
                Console.Out.WriteLine($"Token (expiring: {token.IsExpiring()}): {token.Token}");

                return(DecodeToken(token, cert));
            });

            // Do something with the token. In this case we only wait for it to be decoded and written to the console
            tokenTask.GetAwaiter().GetResult();
        }
Example #2
0
        private static async Task <MaskinportenToken> GetMaskinportenToken(X509Certificate2 x509Certificate2)
        {
            MaskinportenClientConfiguration maskinportenConfig = new MaskinportenClientConfiguration(
                audience: @"https://oidc-ver2.difi.no/idporten-oidc-provider/",           // ID-porten audience path
                tokenEndpoint: @"https://oidc-ver2.difi.no/idporten-oidc-provider/token", // ID-porten token path
                issuer: @"<Integrasjonens identifikator>",                                // Integrasjonens identifikator fra difi Integrasjon i selvbetjeningsportal
                numberOfSecondsLeftBeforeExpire: 10,                                      // The token will be refreshed 10 seconds before it expires
                certificate: x509Certificate2);                                           // Virksomhetssertifikat as a X509Certificate2

            MaskinportenClient maskinportenClient = new MaskinportenClient(maskinportenConfig);
            String             scope       = "ks:fiks"; // Scope for access token
            MaskinportenToken  accessToken = await maskinportenClient.GetAccessToken(scope);

            return(accessToken);
        }