Click here to Skip to main content
15,885,933 members
Articles / Web Development / ASP.NET

Cross Domain/Platform Authentication and Data Transfer

Rate me:
Please Sign up or sign in to vote.
4.87/5 (10 votes)
29 Dec 2008CPOL14 min read 89.9K   917   66  
Introduces a methodology for authenticating user in cross domain/platform and transferring user data from one site to another during the authentication process.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class LandingPage1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
            return;

        //make sure user is not authenticated when the third party program starts.
        FormsAuthentication.SignOut();

        //request for the AuthenticationID
        string AuthenticationID = Request.QueryString["AuthID"];
        if (AuthenticationID == null)
        {
            lblError.Text = "A required parameter is missing from url. ";
            return;
        }
        //Request p1 and p2 from Url. p1 and p2 are the parameters that the third party app needs
        string p1 = Request.QueryString["Para1"].ToString();
        string p2 = Request.QueryString["Para1"].ToString();
        //additional code here to process the parameters

        //Add a web reference to your app and name it anything you like. Here it is named as AuthenticationService. 
        //declare web service and a reference variable - ReturnMessage
        AuthenticationService.AuthenticationService AuthService = new AuthenticationService.AuthenticationService();
        string ReturnMessage = "";
        DataSet ds = null;
        //Call Web Method: RetrieveUserDataSet
        //success: user authenticated, get a DataSet. 
        //Failure: user not authenticated or Url exipred. Return null and error message.
        try
        {
            ds = AuthService.RetrieveUserDataSet(ref ReturnMessage, AuthenticationID);
        }
        catch (Exception ex)
        {
            lblError.Text += ex.Message.ToString();
        }

        if (ReturnMessage != "")
        {
            lblError.Text += ReturnMessage;
            return;
        }
        if (ds != null)
        {
            //depending on the stored procedure used to retrieve the data
            //The DataSet can contain multiple tables 
            //Write code here to loop through the DataSet
            //insert or update the third party database
            //and then programmatically log in the user 
            //by setting up appropriate cookies and session variables
            //For asp.net form authentication, call SetAuthCookie method
            FormsAuthentication.SetAuthCookie("LoginUser", false);

            //display user data for demo purpose
            gvUserData.DataSource = ds.Tables[0];
            gvUserData.DataBind();
        }
        else
        {
            lblError.Text += ReturnMessage;
        }
   
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //display authentication status
        //display authentication status
        if (!Request.IsAuthenticated)
            lblError.Text = "The user is not authenticated";
        else
            lblError.Text = "The user is authenticated";
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Web & Database Developer. Design and implement web and database applications utilizing Microsoft and other development tools.

Comments and Discussions