Wednesday, August 5, 2009

How To: Pass Authentication Between Two ASP.NET Applications

if you have two Web applications and every application hosted in separated Server
also every web Application has Login Page, so how we can save our credentials once we logged on first web application and if I want to go to the second Application without the second application request my authentication again.

there are many solutions for this issue:
but I will talk about one solution and it is "Cookie Solution"
also I will show you how to pass authentications between applications using cookies

1- Create two ASP.NET Web Applications (TestOne - TestTwo);
2- Create Login Page in Every Application
3- Configure web.config File in every application to be support Membership
for more information Read
How To: Use SQL Membership Provider in ASP.NET

4-in TestOne Application in Login Page put the Following Code in (Page_Load) Event:

HttpCookie userName = Request.Cookies.Get("UserName");
HttpCookie password = Request.Cookies.Get("Password");

if (userName != null password != null)
{
if (Membership.ValidateUser(userName.Value, password.Value))
{
FormsAuthentication.RedirectFromLoginPage(userName.Value, false);
}
}

5- then Publish TestOne Web Application in your IIS
6- in TestTwo Web Application in Defualt Page put the following Coed in
(Page Load) Event:

HttpCookie username = new HttpCookie("UserName", "wael");
HttpCookie password = new HttpCookie("Password", "1234!@#$");
Response.Cookies.Add(username);
Response.Cookies.Add(password);
Response.Redirect("http://localhost/waleedelkot/Default.aspx");

then TestTwo Application Will connect to TestOne without request your authentication again.

2 comments: