Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login with gmail account through c# .net

i am using DotNetOpenID dll for logging my sample application through gmail authentication through c# .net

code which i used was

protected void Page_Load(object sender, EventArgs e)
    {
        OpenIdRelyingParty rp = new OpenIdRelyingParty();
        var r = rp.GetResponse();
        if (r != null)
        {            
            switch (r.Status)
            {
                case AuthenticationStatus.Authenticated:
                    NotLoggedIn.Visible = false;
                    Session["GoogleIdentifier"] = r.ClaimedIdentifier.ToString();
                    Response.Redirect("About.aspx"); //redirect to main page of your website  
                    break;
                case AuthenticationStatus.Canceled:
                    lblAlertMsg.Text = "Cancelled.";
                    break;
                case AuthenticationStatus.Failed:
                    lblAlertMsg.Text = "Login Failed.";
                    break;
            }
        }
    }

    protected void OpenLogin_Click(object src, CommandEventArgs e)
    {
        string discoveryUri = e.CommandArgument.ToString();
        OpenIdRelyingParty openid = new OpenIdRelyingParty();
        var b = new UriBuilder(Request.Url) { Query = "" };
        var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri);
        req.RedirectToProvider();
    }

it works well when i click the gmail login button it goes to the gmail page and authenticate as i need.

but my problem is AuthenticationStatus.Authenticated status was failed after authentication always even though i am giving correct username and password of gmail account

Waiting for valuable response and comments

like image 303
GowthamanSS Avatar asked Mar 12 '13 05:03

GowthamanSS


People also ask

How do I access my Gmail account on my PC?

On your computer, go to gmail.com. Enter your Google Account email address or phone number and password. If information is already filled in and you need to sign in to a different account, click Use another account.

How can I access my Gmail account?

Any web browser—Go to mail.google.com. and choose Gmail. Android devices—Install and open the Android app.

How do I access my Gmail account from another computer?

On a computer, open Chrome. Click Guest. Go to a Google service, like www.google.com, and sign in to your account. When you're done using the web, close the "Guest mode" browsing window.


1 Answers

As par your requirement.You should try this code or see this link : Gmail credentials for Authentication of ASP.net Website

protected void Page_Load(object sender, EventArgs e)
{
    OpenIdAjaxRelyingParty rp = new OpenIdAjaxRelyingParty();
    var response = rp.GetResponse();
    if (response != null)
    {
        switch (response.Status)
        {
            case AuthenticationStatus.Authenticated:
                NotLoggedIn.Visible = false;
                Session["GoogleIdentifier"] = response.ClaimedIdentifier.ToString();

                var fetchResponse = response.GetExtension<FetchResponse>();
                Session["FetchResponse"] = fetchResponse;
                var response2 = Session["FetchResponse"] as FetchResponse;

                string UserName = response2.GetAttributeValue(WellKnownAttributes.Name.First) ?? "Guest"; // with the OpenID Claimed Identifier as their username.
                string UserEmail = response2.GetAttributeValue(WellKnownAttributes.Contact.Email) ?? "Guest";   

                Response.Redirect("Default2.aspx");
                break;

            case AuthenticationStatus.Canceled:
                lblAlertMsg.Text = "Cancelled.";
                break;
        }

    }
}
protected void OpenLogin_Click(object sender, CommandEventArgs e)
{

    string discoveryUri = e.CommandArgument.ToString();
    OpenIdRelyingParty openid = new OpenIdRelyingParty();

    var url = new UriBuilder(Request.Url) { Query = "" };
    var request = openid.CreateRequest(discoveryUri); // This is where you would add any OpenID extensions you wanted
    var fetchRequest = new FetchRequest(); // to fetch additional data fields from the OpenID Provider

    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.HomeAddress.Country);
    request.AddExtension(fetchRequest);

    request.RedirectToProvider();

}
like image 157
Anurag Jain Avatar answered Oct 12 '22 07:10

Anurag Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!