PingOne for Enterprise

.NET Token Exchange Sample

using System;
using System.Data;
using System.IO;
using System.Text;
using System.Net;
using System.Configuration;
using System.Collections.Generic;

public partial class ssoLogin : System.Web.UI.Page
{

    private static void SetBasicAuthHeader(HttpWebRequest req, String userName,
        String userPassword)
    {
        string authInfo = userName + ":" + userPassword;
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        req.Headers["Authorization"] = "Basic " + authInfo;
    }

    private static void SetAgentIdCookie(HttpWebRequest req, String agentid)
    {
        if (agentid != null)
        {
            req.Headers["Cookie"] = "agentid=" + agentid;
        }
    }

    private static string GetTokenServiceUrl(string pingConnectToken)
    {
        return "${tokenServiceBaseUrl}/sso/TXS/2.0/2/"+ pingConnectToken;
    }

 /**
 * The restApiKey value is the REST API Client Secret (the password/secret associated with your REST API Client ID). You will need
 * to uncomment the String restApiKey statement and replace the empty string in the sample below with your REST API Client Secret.
 * For example:  String restApiKey = "mySecretApiPassword";
 *
 * The restAuthUsername value is the REST API Client ID (a GUID) automatically assigned to your account in the PingOne admin portal
 * on the Account > Integration page.
 * You will need to replace the restAuthUsername value in "${restAuthUsername}" in the sample below with your REST API Client ID.
 * For example: SetBasicAuthHeader(request, "5f6ce45e-1a00-488e-8519-7c9946cb6379", restApiKey);
 */

protected void Page_Load(object sender, EventArgs e)
    {
        String tokenId = Request.QueryString["tokenid"];
        String agentId = Request.QueryString["agentid"];

        HttpWebRequest request = WebRequest.Create(GetTokenServiceUrl(tokenId))
                            as HttpWebRequest;

        // Specify this value at http://admin.pingidentity.com
        //String restApiKey = "";
        SetBasicAuthHeader(request, "${restAuthUsername}", restApiKey);
        SetAgentIdCookie(request, agentId);
        request.Method = "GET";
        request.ContentType = "text/plain;charset=utf-8";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        String content = reader.ReadToEnd();
        String [] lines = content.Split('\\n');
        Dictionary<string, string> properties = new Dictionary<string, string>();
        foreach (string line in lines )
        {
            if (line.Contains("="))
            {
                properties.Add(line.Split('=')[0], line.Split('=')[1]);
            }
        }
        String subject = properties["pingone.subject"];
        String idpId = properties["pingone.idp.id"];
        CreateUserSession(subject, idpId);
    }

    protected void CreateUserSession(string subject, string idpId)
    {
        // Implement Me! Must validate that subject belongs to this idpId
        Response.Write("<p>Welcome, " + subject + "</p>");
    }
}