How to use OpenID ?

What is OpenID 

If you don't know what OpenID is, then take a look at this short video that explains it very well.
[youtube:xcmY8Pk-qEk]

The goal

 My goal was simple. I wanted to be able to add support for OpenID authentication on existing websites. It means that I wanted to use a standard textbox for entering the OpenID into and a button that would start the whole authentication mechanism. In order to do that, I needed a class that would take care of the communication with the OpenID servers and handle the authentication for me.
 So, I ended up with a single small class that can be dumped into any ASP.NET website. It handles the redirection to the OpenID provider and leaves the authentication handling up to the website, but provides all the relevant information for doing so. The class is called OpenID and is very short and simple.

 Code example

 If you don’t care about the code, but still wants to see how this baby works, check out my OpenID video. You can also download the small code sample at the bottom of the post.
 For the following code sample, imaging you have a page called login.aspx. On that page is a textbox called txtOpenId and a button called btnLogon. The user enters his OpenID in the textbox and clicks the button. The event handler of the button’s click event now sends a login request to the OpenID class. The class then redirects the user to her OpenID provider website and is asked to confirm whether or not she will share her information. She accepts and is returned to the login.aspx page.
 The login page now retrieves the information given by the OpenID provider from the class. Now the login page can have the information needed to perform the login.

Here is the code-behind of the login.aspx.

protected void Page_Load(object sender, EventArgs e)
{
  if (OpenID.IsOpenIdRequest)
  {
    OpenIdData data =  OpenID.Authenticate();
    if (data.IsSuccess)
    {
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("email: {0}
", data.Parameters["email"]);
      sb.AppendFormat("fullname: {0}
", data.Parameters["fullname"]);
      sb.AppendFormat("country: {0}
", data.Parameters["country"]);
      sb.AppendFormat("phone: {0}
", data.Parameters["phone"]);
      sb.AppendFormat("language: {0}
", data.Parameters["language"]);
 
      Response.Write(sb.ToString());
    }
  }
 
  btnLogon.Click +=new EventHandler(btnLogon_Click);
}
 
void btnLogon_Click(object sender, EventArgs e)
{
  bool success = OpenID.Login(txtOpenId.Text, "email,fullname,phone", "country,language");
 
  if (!success)
  {
    Response.Write("The OpenID is not valid");
  }
}
How to use OpenID ? How to use OpenID ? Reviewed by Manan Bhavsar on 9:48 AM Rating: 5