ASP.NET - How to login to some web site from my own one

Asked By mostafa hamdy on 12-Jul-11 11:53 AM
Hello all
I have some web site built in asp.net 4, and I  want to create some Icon on my site to Yahoo mail , Facebook or what ever the site and when I click this Icon I want to login directly to my account in this site with out entering my login info, sure I will register my login data for every site  of them have an Icon, please If any body get what I mean and can help me in doing that please send me
Regards
Mostafa
Ravinder Jamgotre replied to mostafa hamdy on 12-Jul-11 12:03 PM
Hi

you need to use forms authentication and define the username and password for the web url you are attempting to goto; this will pass through the required credentails and allow access. Seems like you are trying to use SSO. there are other solution my onelogin on the web offering clould authentication services. I do not have the code to do this as I found using third party tools was much cleaner. However maybe this site can help further.

http://www.codeproject.com/KB/web-security/aspnetsinglesignon.aspx
http://msdn.microsoft.com/en-us/library/dd577079.aspx
http://stackoverflow.com/questions/2677019/rpxnow-competitors
dipa ahuja replied to mostafa hamdy on 12-Jul-11 12:14 PM
Untitled document
you can add this code to share to any social networking sites from http://www.addthis.com/
 


 
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4da1a59d51aa80c1"></script>
<!-- AddThis Button END -->
 
 

Ravi S replied to mostafa hamdy on 12-Jul-11 02:03 PM
HI

Don't think of it so much as going to the page and filling stuff in. You're going to need to create a http://msdn.microsoft.com/en-us/library/debx8sh9.aspx that contains the proper HTTP headers and data, that will look to their server to be someone that filled everything in. You'll need to look at the site you're trying to do this with for specifics, but I'd assume they're sending over a set of POST data when you click sign in. Example, pulled from MSDN and modified to be closer what you need (first few lines):

WebRequest request = WebRequest.Create ("http:/www.site.com/loginPostback");
request
.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "username=blah;password=blah";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request
.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request
.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream
.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream
.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream
= response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader
.Close ();
dataStream
.Close ();
response
.Close ();
Jitendra Faye replied to mostafa hamdy on 13-Jul-11 12:46 AM

Giving you example in one way is possible that -
http://forums.asp.net/t/1647856.aspx/asp.net/t/1647173.aspx
Sharing Authentication Between Applications

There is nothing to do with coding,we just need to select both websites/application and assign them same pool on IIS Server,Apart you need to make some changes on WebConfig,which you can check on same thread.

Hope this will help you.

mostafa hamdy replied to Ravi S on 18-Jul-11 08:17 AM
Hello Ravi
at the beginning many thanx about your code segment really it helped my so much, but after I write it down with some modifications ,It just open the login page which I pass to it with out doing any login , the code I have done is as the following:

      string LOGIN_URL = "http://www.facebook.com/index.php";

      string USERNAME = "myaccount@yahoo.com";

      string PASSWORD = "mypassword";

 

 

      HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;

      StreamReader responseReader = new StreamReader(

        webRequest.GetResponse().GetResponseStream()

      );

      string responseData = responseReader.ReadToEnd();

     

      responseReader.Close();

      string postData = "email=mostafa_h_wahab@yahoo.com;pass=ekramhamdy";

      byte[] byteArray = Encoding.UTF8.GetBytes(postData);

 

CookieContainer cookies = new CookieContainer();

      webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;

      webRequest.Method = "POST";

      webRequest.ContentType = "application/x-www-form-urlencoded";

      webRequest.CookieContainer = cookies;

      StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());

      requestWriter.Write(postData);

      requestWriter.Close();

      webRequest.GetResponse().Close();

      SECRET_PAGE_URL = "http://www.facebook.com/#!/profile.php?id=100001740129596";

      webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;

      webRequest.CookieContainer = cookies;

      responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

      responseData = responseReader.ReadToEnd();

      responseReader.Close();

      Response.Write(responseData);



please if you get why this code stop at the login page and dont do on on the loging process please send me or tell me about some url may help me in completing that
thanx again 
best regards
Mostafa