How to pass authentication between two ASP.NET applications

How to pass authentication between two ASP.NET applications

If you have two web applications and every application hosted in a separated server, also every web application has login page, so how we can save our credentials once we logged in first web application and if we want to go to the second application without the second application request my authentication again.

There are many solutions for this case, but we will talk about one solution and it is “Cookie Solution”, also we will show you how to pass authentications between applications using cookies

1- Create two ASP.NET web applications (App1- App2).
2- Create login page in each application.
3- Configure web.config file in each application to support membership.
4-in App1 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 App1 web application on your IIS.
6- in App2 web application in Defualt Page put the following coed in (Page Load) Event:

HttpCookie username = new HttpCookie("UserName", "amrsaafan");
HttpCookie password = new HttpCookie("Password", "[email protected]");
Response.Cookies.Add(username);
Response.Cookies.Add(password);
Response.Redirect("http://localhost/App2/Default.aspx");

then App2 application Will connect to App1 without request your authentication again.

Share this post

Leave a Reply

Your email address will not be published.