Here is my code:
aspx page:
.cs page
protected void Page_Load(object sender, EventArgs e)
{
HandleOpenIDProviderResponse();
}
protected void HandleOpenIDProviderResponse()
{
var response = openid.GetResponse();
if (response != null)
{
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
//NotLoggedIn.Visible = false;
//btngmaillogout.Visible = true;
var fetchResponse = response.GetExtension<FetchResponse>();
Session["FetchResponse"] = fetchResponse;
var response2 = Session["FetchResponse"] as FetchResponse;
lblemail.Text = response2.GetAttributeValue(WellKnownAttributes.Contact.Email);
lblname.Text = GetFullname(response2.GetAttributeValue(WellKnownAttributes.Name.First), response2.GetAttributeValue(WellKnownAttributes.Name.Last));
lblbirthdate.Text = response2.GetAttributeValue(WellKnownAttributes.BirthDate.WholeBirthDate);
lblphone.Text = response2.GetAttributeValue(WellKnownAttributes.Contact.Phone.Mobile);
lblgender.Text = response2.GetAttributeValue(WellKnownAttributes.Person.Gender);
Session["Email"] = lblemail.Text;
Session["UserName"] = lblname.Text;
value = Session["UserName"].ToString();
break;
}
}
else
{
return;
}
}
protected void btnGmail_Click(object sender, CommandEventArgs e)
{
string discoveryUri = e.CommandArgument.ToString();
var b = new UriBuilder("https://www.facebook.com/") { Query = "" };
var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri);
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Person.Gender);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Phone.Mobile);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.BirthDate.WholeBirthDate);
req.AddExtension(fetchRequest);
req.RedirectToProvider();
}
private static string GetFullname(string first, string last)
{
var _first = first ?? "";
var _last = last ?? "";
if (string.IsNullOrEmpty(_first) || string.IsNullOrEmpty(_last))
return "";
return _first + " " + _last;
}
Getting error in btnGmail_Click Event,
var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri); //in this line getting error

krishna bharathPosted Aug 26, 2014, 2:28 AM
I have followed your post and used it in my application. I am able to retrieve the email, first name, last name and the country. I am not able to get the phone number, gender, date of birth, image etc. Please help me in retrieving those details also.
Thanks In Advance.
Ankur JainPosted Aug 11, 2014, 11:46 PM
Above code is for fetching user's private details with user permission...but you can redirect to another page after login is authenticated. see in above code there is a switch case in
HandleOpenIDProviderResponse() method where you can see there is a three case is
case AuthenticationStatus.Authenticated:
case AuthenticationStatus.Canceled:
and case AuthenticationStatus.Failed:
in case AuthenticationStatus.Authenticated:
i am fetching and display data in label like: lblemail.Text = response2.GetAttributeValue(WellKnownAttributes.Contact.Email);
you can save these response in database and then you can write redirect code ....
Sarath KumarPosted Aug 11, 2014, 11:51 AM
Where can i given the Redirect url.
Ankur JainPosted Aug 11, 2014, 5:34 AM
try this code...
Suhas DPosted Aug 11, 2014, 5:23 AM
http://forums.asp.net/t/1767432.aspx?Gmail+credentials+for+Authentication+of+ASP+net+Website
http://www.fryan0911.com/2010/09/use-google-openid-authentication-in.html