This class provides methods for OpenID authentication process. It can be used with OAuth class for hybrid OpenID + OAuth authentication.
For a list of all members of this type, see OpenID Members.
System.Object
MailBee.OpenID
Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.
OpenID + OAuth hybrid method makes it possible to access certain data (like e-mails) in the user account without requiring the user to enter their e-mail address and password, provided that the user is already logged into their account by other means.
For instance, if the user is logged in their Gmail account on that computer and the user accesses your web application, it can get access to the e-mails in that account if the user permits this. The user just needs to click a button (confirmation from Google to grant access to the user's data to your web application), it's not required to enter the login or password. Also, your application does not get access to your account's password, only to your account's data.
To make requests for approval, your application must be registered on the web site of a service provider. E.g. if you want to let users grant you with the access to their Gmail mailboxes, you'll need to register your application with Google, or with Yahoo in order to access Yahoo Mail accounts. In return, the service provider assigns you a consumer key and consumer secret you'll need to pass to the service each time you make a request for a user's data.
See "Federated Login for Google Account Users" on Google web site for additional information on how to register your site in Google.
Note This class provides helper classes which support OpenID + OAuth implementations of Google and Yahoo. You can, however, add any other implementation which sets OpenID/OAuth parameters as required by your service provider. For that, create and populate parameters object in the samples below not using GetParamsForGoogleOpenIdWithOAuth or GetParamsForYahooOpenIdWithOAuth methods. Or, you can use these methods and then adjust the resulting object in case if the implementation you're working with is very close to Google or Yahoo (for instance, simply requires adding an extra parameter).
This topic contains samples for both Google (Gmail) and Yahoo below.
The first web sample authenticates client in Gmail IMAP via OpenID + OAuth authentication. It has two pages: "Default.aspx" and "WebForm1.aspx". User opens "Default.aspx" and is get redirected to OpenID server page on Google web site (where the user can grant access to their data to your application). Then, Google redirects the user's browser to "WebForm1.aspx" which actually accesses the user's e-mail account via IMAP.
"http://[yourhost]/hybrid/WebForm1.aspx" designates the URL of "WebForm1.aspx" page (so that Google would know where it should redirect the user after passing the authorization stage).
If user sets "Remember this approval" checkbox on Google authentication page, next time Google will not ask to allow site request for getting Gmail access rights and will get access by default. GMail XOAuth key also may be stored by application for logging in GMail without authentication process.
[C#] // Default.aspx.cs // To use the code below, import the following namespaces at the top of your code. using MailBee; // The actual code (put it into a method of your class). // Get OpenID server name. string openIdSrv = OpenID.GetOpenIDServer("https://www.google.com/accounts/o8/id"); StringDictionary parameters = OpenID.GetParamsForGoogleOpenIdWithOAuth(); parameters["openid.return_to"] = "http://[yourhost]/hybrid/WebForm1.aspx"; parameters["openid.realm"] = "http://[yourhost]/hybrid/"; parameters["openid.oauth.consumer"] = "[consumer_key]"; Response.Redirect(OpenID.GetRedirectURL(openIdSrv, parameters)); // WebForm1.aspx.cs // To use the code below, import the following namespaces at the top of your code. using MailBee; using MailBee.ImapMail; // The actual code (put it into a method of your class). // Input consumerKey/consumerSecret from Google here. string consumerKey = ""; string consumerSecret = ""; OAuth myOAuth = new OAuth(consumerKey, consumerSecret); // Enables OpenID + OAuth extension. myOAuth.EnableOpenIDHybrid = true; // Request OAuth access key from myOAuth.AccessToken("https://www.google.com/accounts/OAuthGetAccessToken", Request.QueryString["openid.ext2.request_token"]); Response.Write("<b>Access Token:</b> " + myOAuth.Token + "<br/>"); Response.Write("<b>Access Token Secret:</b> " + myOAuth.TokenSecret + "<br/><br/>"); string imapXOAuthKey = myOAuth.GetXOAuthKey( string.Format("https://mail.google.com/mail/b/{0}/imap/", Request.QueryString["openid.ext1.value.email"])); Response.Write("<b>GMail XOAuth key:</b> " + imapXOAuthKey + "<br/><br/>"); // Last: Login to Gmail IMAP server using XOAuth. Imap imp = new Imap(); // Connect to the server, login and select inbox. imp.Connect("imap.gmail.com", 993); imp.Login(null, imapXOAuthKey, AuthenticationMethods.SaslOAuth, AuthenticationOptions.None, null); imp.SelectFolder("INBOX"); Response.Write("<b>Gmail connected successfully via OpenID+XOAuth</b>"); // Processing of INBOX emails here. //... imp.Disconnect();
[Visual Basic] ' Default.aspx.vb ' To use the code below, import the following namespaces at the top of your code. Imports MailBee ' The actual code (put it into a method of your class). ' Get OpenID server name. Dim openIdSrv As String = OpenID.GetOpenIDServer("https://www.google.com/accounts/o8/id") Dim parameters As StringDictionary = OpenID.GetParamsForGoogleOpenIdWithOAuth() parameters("openid.return_to") = "http://[yourhost]/hybrid/WebForm1.aspx" parameters("openid.realm") = "http://[yourhost]/hybrid/" parameters("openid.oauth.consumer") = "[consumer_key]" Response.Redirect(OpenID.GetRedirectURL(openIdSrv, parameters)) ' WebForm1.aspx.vb ' To use the code below, import the following namespaces at the top of your code. Imports MailBee Imports MailBee.ImapMail ' The actual code (put it into a method of your class). ' Input consumerKey/consumerSecret from Google here. Dim consumerKey As String = "" Dim consumerSecret As String = "" Dim myOAuth As OAuth = New OAuth(consumerKey, consumerSecret) ' Enables OpenID + OAuth extension. myOAuth.EnableOpenIDHybrid = True ' Request OAuth access key from myOAuth.AccessToken("https://www.google.com/accounts/OAuthGetAccessToken", _ Request.QueryString("openid.ext2.request_token")) Response.Write("<b>Access Token:</b> " + myOAuth.Token + "<br/>") Response.Write("<b>Access Token Secret:</b> " + myOAuth.TokenSecret + "<br/><br/>") Dim imapXOAuthKey As String = myOAuth.GetXOAuthKey( _ String.Format("https://mail.google.com/mail/b/{0}/imap/", Request.QueryString("openid.ext1.value.email"))) Response.Write("<b>GMail XOAuth key:</b> " + imapXOAuthKey + "<br/><br/>") ' Last: Login to Gmail IMAP server using XOAuth. Dim imp As Imap = New Imap ' Connect to the server, login and select inbox. imp.Connect("imap.gmail.com", 993) imp.Login(Nothing, imapXOAuthKey, AuthenticationMethods.SaslOAuth, AuthenticationOptions.None, Nothing) imp.SelectFolder("INBOX") Response.Write("<b>Gmail connected successfully via OpenID+XOAuth</b>") ' Processing of INBOX emails here. '... imp.Disconnect()
This web sample authenticates the client in Yahoo via OpenID + OAuth authentication. It works in much the same way as Google/Gmail version but it does not include the last step of connecting via IMAP (currently, Yahoo only supports HTTP access for OpenID + OAuth, such as for accessing Yahoo web services, although this may change in the future).
See "Yahoo! OAuth Quick Start Guide" on Yahoo web site for additional information on how to register your site in Yahoo.
[C#] // Default.aspx.cs // To use the code below, import the following namespaces at the top of your code. using MailBee; string openIdSrv = "https://open.login.yahooapis.com/openid/op/auth"; StringDictionary parameters = OpenID.GetParamsForYahooOpenIdWithOAuth(); parameters["openid.return_to"] = "http://[yourhost]/hybrid/WebForm1.aspx"; parameters["openid.realm"] = "http://[yourhost]/hybrid/"; parameters["openid.oauth.consumer"] = "[consumer_key]"; Response.Redirect(OpenID.GetRedirectURL(openIdSrv, parameters)); // WebForm1.aspx.cs // To use the code below, import the following namespaces at the top of your code. using MailBee; // Input consumerKey/consumerSecret from Yahoo here. string consumerKey = ""; string consumerSecret = ""; OAuth myOAuth = new OAuth(consumerKey, consumerSecret); // Enables OpenID + OAuth extension. myOAuth.EnableOpenIDHybrid = true; myOAuth.AccessToken("https://api.login.yahoo.com/oauth/v2/get_token", Request.QueryString["openid.oauth.request_token"]); Response.Write("<b>Access Token:</b> " + myOAuth.Token + "<br/>"); Response.Write("<b>Access Token Secret:</b> " + myOAuth.TokenSecret);
[Visual Basic] ' Default.aspx.vb ' To use the code below, import the following namespaces at the top of your code. Imports MailBee ' The actual code (put it into a method of your class). Dim openIdSrv As String = "https://open.login.yahooapis.com/openid/op/auth" Dim parameters As StringDictionary = OpenID.GetParamsForYahooOpenIdWithOAuth() parameters("openid.return_to") = "http://[yourhost]/hybrid/WebForm1.aspx" parameters("openid.realm") = "http://[yourhost]/hybrid/" parameters("openid.oauth.consumer") = "[consumer_key]" Response.Redirect(OpenID.GetRedirectURL(openIdSrv, parameters)) ' WebForm1.aspx.vb ' To use the code below, import the following namespaces at the top of your code. Imports MailBee ' The actual code (put it into a method of your class). ' Input consumerKey/consumerSecret from Yahoo here. Dim consumerKey As String = "" Dim consumerSecret As String = "" Dim myOAuth As OAuth = New OAuth(consumerKey, consumerSecret) ' Enables OpenID + OAuth extension. myOAuth.EnableOpenIDHybrid = True myOAuth.AccessToken("https://api.login.yahoo.com/oauth/v2/get_token", _ Request.QueryString("openid.oauth.request_token")) Response.Write("<b>Access Token:</b> " + myOAuth.Token + "<br/>") Response.Write("<b>Access Token Secret:</b> " + myOAuth.TokenSecret)
Namespace: MailBee
Assembly: MailBee.NET (in MailBee.NET.dll)
OpenID Members | MailBee Namespace