2013-03-12 9 views
6

sto usando DotNetOpenID dll per l'accesso la mia applicazione di esempio tramite l'autenticazione gmail tramite C# .NETil login in account Gmail tramite C# .NET

codice che ho usato era

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(); 
    } 

funziona bene quando scatto il pulsante di accesso di Gmail va alla pagina di Gmail e autentica come ho bisogno.

ma il mio problema è stato AuthenticationStatus.Authenticated stato fallito dopo l'autenticazione sempre anche se io sto dando corretto nome utente e la password di account gmail

In attesa di risposta prezioso e commenti

risposta

0

io non sono a conoscenza con Dll DotNetOpenID. Tuttavia, consiglierei di usare Fiddler per eseguire un'acquisizione dei dati nel POST che viene inviato durante l'accesso e assicurarsi di inviare il contenuto corretto nel tuo post. C# fornisce HttpWebRequestclass e HttpWebResponseclass in System.Net. C'è qualche ragione per cui non stai usando questi dal System.dll, invece?

Assicurarsi che quando si ripristinano i cookie dal POST che li si inserisce nella raccolta cookie per qualsiasi richiesta successiva.

C'è una bella classe di esempio per gestire le richieste in questo post risposta da cemento

3

Come par tuo requirement.You dovrebbe provare questo codice o vedere questo 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(); 

} 
+0

C'è un modo per disconnettersi una volta che abbiamo effettuato l'autenticazione con successo in OpenId. Per favore aiuto..? – Sutirth

+0

vedi questi link..http: //stackoverflow.com/questions/18530457/logout-with-gmail-account-through-c-sharp-net –

Problemi correlati