Sample code for 30+ languages & platforms
SQL Server

Azure Fetch OpenID Connect metadata document

See more OIDC Examples

Downloads the OpenID Connect self-discovery document for an Azure OIDC enabled app.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'Accept', 'application/json'

    -- See the Microsoft Azure OIDC documentation at https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-protocols-oidc
    -- The "tenant" can take one of four values described in the documentation at the link above.

    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'tenant', '6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd'
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://login.microsoftonline.com/{$tenant}/v2.0/.well-known/openid-configuration', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END


    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'Response Status Code: ' + @iTmp0

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @json, 'Load', @success OUT, @sTmp0
    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    -- Sample output...
    -- (See the parsing code below..)
    -- 
    -- Use the this online tool to generate parsing code from sample JSON: 
    -- Generate Parsing Code from JSON

    -- {
    --   "token_endpoint": "https://login.microsoftonline.com/6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd/oauth2/v2.0/token",
    --   "token_endpoint_auth_methods_supported": [
    --     "client_secret_post",
    --     "private_key_jwt",
    --     "client_secret_basic"
    --   ],
    --   "jwks_uri": "https://login.microsoftonline.com/6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd/discovery/v2.0/keys",
    --   "response_modes_supported": [
    --     "query",
    --     "fragment",
    --     "form_post"
    --   ],
    --   "subject_types_supported": [
    --     "pairwise"
    --   ],
    --   "id_token_signing_alg_values_supported": [
    --     "RS256"
    --   ],
    --   "response_types_supported": [
    --     "code",
    --     "id_token",
    --     "code id_token",
    --     "id_token token"
    --   ],
    --   "scopes_supported": [
    --     "openid",
    --     "profile",
    --     "email",
    --     "offline_access"
    --   ],
    --   "issuer": "https://login.microsoftonline.com/6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd/v2.0",
    --   "request_uri_parameter_supported": false,
    --   "userinfo_endpoint": "https://graph.microsoft.com/oidc/userinfo",
    --   "authorization_endpoint": "https://login.microsoftonline.com/6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd/oauth2/v2.0/authorize",
    --   "device_authorization_endpoint": "https://login.microsoftonline.com/6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd/oauth2/v2.0/devicecode",
    --   "http_logout_supported": true,
    --   "frontchannel_logout_supported": true,
    --   "end_session_endpoint": "https://login.microsoftonline.com/6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd/oauth2/v2.0/logout",
    --   "claims_supported": [
    --     "sub",
    --     "iss",
    --     "cloud_instance_name",
    --     "cloud_instance_host_name",
    --     "cloud_graph_host_name",
    --     "msgraph_host",
    --     "aud",
    --     "exp",
    --     "iat",
    --     "auth_time",
    --     "acr",
    --     "nonce",
    --     "preferred_username",
    --     "name",
    --     "tid",
    --     "ver",
    --     "at_hash",
    --     "c_hash",
    --     "email"
    --   ],
    --   "kerberos_endpoint": "https://login.microsoftonline.com/6d8ddd66-68d1-44b0-af5c-e31b4b7ee5cd/kerberos",
    --   "tenant_region_scope": "NA",
    --   "cloud_instance_name": "microsoftonline.com",
    --   "cloud_graph_host_name": "graph.windows.net",
    --   "msgraph_host": "graph.microsoft.com",
    --   "rbac_url": "https://pas.windows.net"
    -- }

    DECLARE @strVal nvarchar(4000)

    DECLARE @token_endpoint nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @token_endpoint OUT, 'token_endpoint'
    DECLARE @jwks_uri nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @jwks_uri OUT, 'jwks_uri'
    DECLARE @issuer nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @issuer OUT, 'issuer'
    DECLARE @request_uri_parameter_supported int
    EXEC sp_OAMethod @json, 'BoolOf', @request_uri_parameter_supported OUT, 'request_uri_parameter_supported'
    DECLARE @userinfo_endpoint nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @userinfo_endpoint OUT, 'userinfo_endpoint'
    DECLARE @authorization_endpoint nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @authorization_endpoint OUT, 'authorization_endpoint'
    DECLARE @device_authorization_endpoint nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @device_authorization_endpoint OUT, 'device_authorization_endpoint'
    DECLARE @http_logout_supported int
    EXEC sp_OAMethod @json, 'BoolOf', @http_logout_supported OUT, 'http_logout_supported'
    DECLARE @frontchannel_logout_supported int
    EXEC sp_OAMethod @json, 'BoolOf', @frontchannel_logout_supported OUT, 'frontchannel_logout_supported'
    DECLARE @end_session_endpoint nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @end_session_endpoint OUT, 'end_session_endpoint'
    DECLARE @kerberos_endpoint nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @kerberos_endpoint OUT, 'kerberos_endpoint'
    DECLARE @tenant_region_scope nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @tenant_region_scope OUT, 'tenant_region_scope'
    DECLARE @cloud_instance_name nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @cloud_instance_name OUT, 'cloud_instance_name'
    DECLARE @cloud_graph_host_name nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @cloud_graph_host_name OUT, 'cloud_graph_host_name'
    DECLARE @msgraph_host nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @msgraph_host OUT, 'msgraph_host'
    DECLARE @rbac_url nvarchar(4000)
    EXEC sp_OAMethod @json, 'StringOf', @rbac_url OUT, 'rbac_url'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'token_endpoint_auth_methods_supported'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'token_endpoint_auth_methods_supported[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'response_modes_supported'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'response_modes_supported[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'subject_types_supported'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'subject_types_supported[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'id_token_signing_alg_values_supported'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'id_token_signing_alg_values_supported[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'response_types_supported'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'response_types_supported[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'scopes_supported'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'scopes_supported[i]'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'claims_supported'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'claims_supported[i]'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @json


END
GO