OAuthWeb ClassNemiro.OAuth
Represents helper class for sessions management of OAuth.
Inheritance Hierarchy

System Object
  Nemiro.OAuth OAuthWeb

Namespace: Nemiro.OAuth
Assembly: Nemiro.OAuth (in Nemiro.OAuth.dll) Version: 1.9.4.725 (1.9.4.725)
Syntax

public static class OAuthWeb
Methods

  NameDescription
Public methodStatic memberGetAuthorizationUrl(String)
Returns the authorization URL of the specified provider.
Public methodStatic memberGetAuthorizationUrl(String, NameValueCollection)
Returns the authorization URL of the specified provider with specified parameters.
Public methodStatic memberGetAuthorizationUrl(String, String)
Returns the authorization URL of the specified provider and return URL.
Public methodStatic memberGetAuthorizationUrl(String, NameValueCollection, String)
Returns the authorization URL of the specified provider, query parameters and return URL.
Public methodStatic memberGetAuthorizationUrl(ClientName, NameValueCollection, String)
Returns the authorization URL of the specified provider, query parameters and return URL.
Public methodStatic memberRedirectToAuthorization(String)
Redirects current client to the authorization page of the specified provider.
Public methodStatic memberRedirectToAuthorization(String, NameValueCollection)
Redirects current client to the authorization page of the specified provider with specified parameters.
Public methodStatic memberRedirectToAuthorization(String, String)
Redirects current client to the authorization page of the specified provider and return URL.
Public methodStatic memberRedirectToAuthorization(String, NameValueCollection, String)
Redirects current client to the authorization page of the specified provider, query parameters and return URL.
Public methodStatic memberRedirectToAuthorization(ClientName, NameValueCollection, String)
Redirects current client to the authorization page of the specified provider, query parameters and return URL.
Public methodStatic memberVerifyAuthorization 
Verifies the authorization results for the current URL.
Public methodStatic memberVerifyAuthorization(String)
Verifies the authorization results for the specified URL.
Public methodStatic memberVerifyAuthorization(String, String)
Verifies the authorization results for the specified request identifier and the code of the authorization.
Public methodStatic memberVerifyAuthorizationAndRemoveRequest 
Verifies the authorization results for the current URL and removes the request from memory.
Public methodStatic memberVerifyAuthorizationAndRemoveRequest(String)
Verifies the authorization results for the specified URL, and removes the request from memory.
Public methodStatic memberVerifyAuthorizationAndRemoveRequest(String, String)
Verifies the authorization results for the specified request identifier and the code of the authorization, and removes the request from memory.
Top
Remarks

Mainly the class is intended for web projects.

But you can use some methods of the class in desktop applications together with the WebBrowser control.

The methods RedirectToAuthorization(String), RedirectToAuthorization(String, String), RedirectToAuthorization(String, NameValueCollection, String) and VerifyAuthorization  will not work in desktop applications.

Examples

ASP .NET WebForms

The following example shows how to use the TwitterClient in ASP .NET WebForms.

To redirect the user to the login page is used the RedirectToAuthorization(String, String) method.

Processing of the authorization results is performed by VerifyAuthorization .

To test the example, create a new ASP .NET WebForms (empty) project. Add Global.asax.

In the Application_Start event handler (Global.asax file) is registered the TwitterClient.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Nemiro.OAuth;
using Nemiro.OAuth.Clients;

namespace Test.CSharp.AspWebForms
{
  public class Global : System.Web.HttpApplication
  {
    protected void Application_Start(object sender, EventArgs e)
    {
      OAuthManager.RegisterClient
      (
        new TwitterClient
        (
          "cXzSHLUy57C4gTBgMGRDuqQtr",
          "3SSldiSb5H4XeEMOIIF4osPWxOy19jrveDcPHaWtHDQqgDYP9P"
        )
      );
    }
  }
}

Add ExternalLoginResult.aspx.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExternalLoginResult.aspx.cs" Inherits="Test.CSharp.AspWebForms.ExternalLoginResult" %gt;

And add the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nemiro.OAuth;

namespace Test.CSharp.AspWebForms
{
  public partial class ExternalLoginResult : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Write("<pre>");
      var result = OAuthWeb.VerifyAuthorization();
      if (result.IsSuccessfully)
      {
        var user = result.UserInfo;
        Response.Write(String.Format("User ID: {0}<br />", user.UserId));
        Response.Write(String.Format("Name:    {0}<br />", user.DisplayName));
      }
      else
      {
        Response.Write(result.ErrorInfo.Message);
      }
      Response.Write("</pre>");
    }
  }
}

Add Default.aspx and insert one LinkButton to the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.CSharp.AspWebForms.Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <asp:LinkButton ID="lnkTwitter" runat="server" 
        Text="Log in with Twitter" onclick="lnkTwitter_Click" /> 
    </div> 
    </form> 
</body> 
</html>

Add a handler for a click on the link.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nemiro.OAuth;

namespace Test.CSharp.AspWebForms
{
  public partial class Default : System.Web.UI.Page
  {
    protected void lnkTwitter_Click(object sender, EventArgs e)
    {
      OAuthWeb.RedirectToAuthorization("Twitter", new Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri);
    }
  }
}

Windows Forms

The following example shows how to use the MailRuClient in desktop applications.

Methods redirection in Windows Forms applications do not work. To get the address of the authorization GetAuthorizationUrl(String) is used.

To test the example, create a new Windows Forms project with two forms. Add a Button to the Form1.

public Form1()
{
  InitializeComponent();
  button1.Click += new EventHandler(button1_Click);
}

private void Form1_Load(object sender, EventArgs e)
{
  // mail.ru client registration
  OAuthManager.RegisterClient
  (
    new MailRuClient
    (
      "722701", 
      "d0622d3d9c9efc69e4ca42aa173b938a"
    )
  );
}

private void button1_Click(object sender, EventArgs e)
{
  var frm = new Form2();
  frm.ShowDialog();
}

Add a WebBrowser to the Form2.

public Form2()
{
  InitializeComponent();
  webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
  webBrowser1.Navigate(OAuthWeb.GetAuthorizationUrl("Mail.Ru"));
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  // waiting for results 
  if (e.Url.Query.IndexOf("code=") != -1 || e.Url.Fragment.IndexOf("code=") != -1 || e.Url.Query.IndexOf("oauth_verifier=") != -1)
  {
    // is the result, verify 
    var result = OAuthWeb.VerifyAuthorization(e.Url.ToString());
    if (result.IsSuccessfully)
    {
      // show user info
      MessageBox.Show
      (
        String.Format
        (
          "User ID: {0}\r\nUsername: {1}\r\nDisplay Name: {2}\r\nE-Mail: {3}", 
          result.UserInfo.UserId,
          result.UserInfo.UserName,
          result.UserInfo.DisplayName ?? result.UserInfo.FullName,
          result.UserInfo.Email
        ), 
        "Successfully", 
        MessageBoxButtons.OK, 
        MessageBoxIcon.Information
      );
    }
    else
    {
      // show error message
      MessageBox.Show(result.ErrorInfo.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    this.Close();
  }
}
See Also