Click here to Skip to main content
15,881,803 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.8K   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.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;


public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblReturnUrl.Text =( Request.QueryString["ReturnUrl"]??"").ToString();
            lblSiteID.Text = (Request.QueryString["SiteID"] ?? "").ToString();
            if (lblReturnUrl.Text == ""||lblSiteID.Text=="")
            {
                lblError.Text = "The ReturnUrl or SiteID is missing. Can't proceed.";
                btnLogin.Enabled = false;
                return;
            }
            txtUserName.Focus();
            //provide testing accounts for user to log in
            lblError.Text = "Login credentials:<br>user: johnd    pw: password or user: janed   pw: password";
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable tbl = Authentication.VerifyCredentials(txtUserName.Text, txtPassword.Text);
            
            if (tbl.Rows.Count>0)
            {
                int MinutesToExpire = 1;//Authentication will expire in 1 minute. Can be set to other values
                ProcessAuthenticationData(tbl, MinutesToExpire, Convert.ToInt16(lblSiteID.Text), lblReturnUrl.Text);

            }
            else
            {
                lblError.Text = "No user with these credentials has been found.";
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message.ToString();
        }

    }
    private void ProcessAuthenticationData(DataTable tbl, int minutesToExpire, int siteID, string returnUrl)
    {
        //place user data into a collection which is easy to handle
        NameValueCollection UserData = Authentication.BuildUserDataCollection(tbl, minutesToExpire, siteID, returnUrl);

        //build all data into a text string so that it can be stored in database
        string UserString = Authentication.SerializeParameters(UserData);

        //Save querystring parameter and user data string
        string AuthenticationID = Guid.NewGuid().ToString().Replace("-", "");
        //save user information. When the third party app calls web service, this information will be retrieved
        Authentication.ExecuteNonQuery("AuthenticationLog_Insert", new object[] { AuthenticationID, UserString});
        
        Response.Redirect(Authentication.WellFormReturnUrl(lblReturnUrl.Text, AuthenticationID));
    }
}

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