barryokane
5/17/2012 - 11:08 AM

SSO Login for Freshdesk support portal - ASP.Net C# Sample Code

SSO Login for Freshdesk support portal - ASP.Net C# Sample Code

protected void Page_Load(object sender, EventArgs e)
{
        string url = GetSsoUrl(ConfigurationManager.AppSettings["FreshDesk.BaseUrl"], //including trailing slash
                               ConfigurationManager.AppSettings["FreshDesk.Secert"], user.UserName, user.Email);
        Response.Redirect(url);
    }

string GetSsoUrl(string baseUrl, string secert, string name, string email)
{
    return String.Format("{0}login/sso/?name={1}&email={2}&hash={3}", baseUrl, Server.UrlEncode(name),
                         Server.UrlEncode(email), GetHash(secert, name, email));
}

static string GetHash(string secert, string name, string email)
{
    string input = name + email + secert;

    MD5 md5 = System.Security.Cryptography.MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
    byte[] hash = md5.ComputeHash(inputBytes);

    StringBuilder sb = new StringBuilder();
    foreach (byte b in hash)
    {
        string hexValue = b.ToString("X").ToLower(); // Lowercase for compatibility on case-sensitive systems
        sb.Append((hexValue.Length == 1 ? "0" : "") + hexValue);
    }
    return sb.ToString();
}