Tuesday, December 1, 2009

.NET Authentication Process for Server

Authentication Process in Dataset

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
//using [Web Service Name];

public partial class _Default : System.Web.UI.Page
{
public DataSet ds_GetPickUpDropOffInDataSet = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
try
{
ParveenAPIService pas = new ParveenAPIService();
Authentication auth = new Authentication();

auth.UserId = "1023";
auth.UserName = "hema";
auth.Password = "54321";
pas.AuthenticationValue = auth;

ds_GetPickUpDropOffInDataSet = pas.GetPickUpDropOffInDataSet();
//The HTMLEncode method applies HTML encoding to a specified string.
//DataSet..::.GetXml Method returns the XML representation of the data
//stored in the DataSet.
Response.Write(Server.HtmlEncode(ds_GetPickUpDropOffInDataSet.GetXml()));
}
catch (Exception err)
{
Response.Write(err);
}

}
}

.NET Client and Server [HTTP Request and Response]

Client Code

//Adding Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Reflection;
using System.Net;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
public string url = null;
public NameValueCollection coll;


protected void Page_Load(object sender, EventArgs e)
{
try
{
url = "http://localhost:____.aspx";
System.Net.HttpWebRequest webrequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
webrequest.Method = "GET";
System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
headers.Add("pvalue", "12345");
//Adding header name and value to a URL
webrequest.Headers = headers;
System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webrequest.GetResponse();


// Get the response stream
StreamReader reader = new StreamReader(webResponse.GetResponseStream());
// Read the whole contents and return as a string
string result = reader.ReadToEnd();
//Print the contents
Response.Write(result);
}
catch (Exception err)
{
Response.Write(err);
}
}
}


Server Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
//using [Web Service Name];
using check; //check is namespace name

public partial class _Default : System.Web.UI.Page
{
public string str_GetPickUpDropOffInString = null;

protected void Page_Load(object sender, EventArgs e)
{
//GetPickUpDropOffInString()
try
{
// Authentication Process
ParveenAPIService pas = new ParveenAPIService();
Authentication auth = new Authentication();

auth.UserId = "1026";
auth.UserName = "hema";
auth.Password = "54321";
pas.AuthenticationValue = auth;


//Checking for private key
//Creating an object for class
Check_Class pkobj = new Check_Class();
//Accessing the method Check_Code using the object pkobj and storing it in a string.
//Request.Headers["pvalue"] is for requesting the header's
//value by specifying the header name.
string pk=pkobj.Check_Code(Request.Headers["pvalue"].ToString());

if (pk == "True") //Private key specified
{
str_GetPickUpDropOffInString = pas.GetPickUpDropOffInString();
Response.Write(str_GetPickUpDropOffInString);
}
else
{
Response.Write("Invalid User");
}
}
catch (Exception err)
{
Response.Write(err);
}
}
}