PureCloudOAuthControl 2.0.0

Suggested Alternatives

GenesysCloudOAuthWebView

Additional Details

Microsoft has ended support for Internet Explorer and this package has been deprecated. Please use GenesysCloudOAuthWebView.

There is a newer version of this package available.
See the version list below for details.
dotnet add package PureCloudOAuthControl --version 2.0.0
NuGet\Install-Package PureCloudOAuthControl -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PureCloudOAuthControl" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PureCloudOAuthControl --version 2.0.0
#r "nuget: PureCloudOAuthControl, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PureCloudOAuthControl as a Cake Addin
#addin nuget:?package=PureCloudOAuthControl&version=2.0.0

// Install PureCloudOAuthControl as a Cake Tool
#tool nuget:?package=PureCloudOAuthControl&version=2.0.0

PureCloud API - .NET OAuth Control

NuGet Badge

Overview

This project produces a .NET library that provides a simple way to execute an OAuth 2 flow in a C# application. This is accomplished by providing the OAuthWebBrowser class, which inherits from the WebBrowser winforms control. When invoked, the control will navigate to the appropriate login URL, allow the user to authenticate securely, and will raise events for authentication and error events.

Install Using nuget

install-package PureCloudOAuthControl

Getting Started

Example Applications

This solution contains three examples projects. The Oauth Example project uses the OAuthWebBrowser control in a winforms app. The OAuth Example WPF project uses the OAuthWebBrowser control in a WPF by making use of the WindowsFormsHost class. The OAuth Standalone Form Example project uses the OAuthWebBrowserForm winform in a console application.

Using OAuthWebBrowser

Referencing the Library

If you've used the Package Manager Console to install the package, there are no additional steps.

If you're building from source or otherwise not using nuget, reference your version of ININ.PureCloud.OAuthControl.dll in your project and add a reference or install the package for RestSharp.

Using the Library

Creating an Browser Instance

To create just the web browser control, use UI tools to add the control or create it in code (must create in code for WPF):

var browser = new OAuthWebBrowser();
Setting the Environment

The browser control will use the mypurecloud.com environment by default. If the user should log in to an org in another region, simply specify that region in the browser's Environment property prior to initiating the login flow. There is no validation on the values, but should be one of "mypurecloud.com", "mypurecloud.com.au", "mypurecloud.ie", or "mypurecloud.jp" as of the time of this writing. If additional regions are added, simply use the new URL in the same partial format.

var browser = new OAuthWebBrowser();
browser.Environment = "mypurecloud.ie";
Using the Standalone Form

The easiest method of use for this control is to use the OAuthWebBrowserForm form to handle authentication with just a few lines of code. This example will instiantiate and configure the form, initiate the login process, and log the result:

// Create form
var form = new OAuthWebBrowserForm();

// Set settings
form.oAuthWebBrowser1.ClientId = "babbc081-0761-4f16-8f56-071aa402ebcb";
form.oAuthWebBrowser1.RedirectUriIsFake = true;
form.oAuthWebBrowser1.RedirectUri = "http://localhost:8080";

// Open it
var result = form.ShowDialog();

Console.WriteLine($"Result: {result}");
Console.WriteLine($"AccessToken: {form.oAuthWebBrowser1.AccessToken}");

Notes

  • The form has overridden the methods Show(), Show(IWin32Window), ShowDialog(), and ShowDialog(IWin32Window) to validate the configuration (client ID and redirect URI properties must be set), begin the implicit grant flow, and set the dialog result based on if there is an access token (return string.IsNullOrEmpty(oAuthWebBrowser1.AccessToken) ? DialogResult.Cancel : DialogResult.OK;).
  • The form does not proxy properties or events from the browser. The browser object can be directly accessed via OAuthWebBrowserForm.oAuthWebBrowser1.
  • When using this in a console application, remember that the application must be decorated with [STAThread] to interact with UI components.
  • The Show and ShowDialog methods will log all exceptions to the console and rethrow them. Handle the custom exception type OAuthSettingsValidationException to identify validation errors.

Setup

The following properties should be configured before invoking an OAuth flow:

  • RedirectUri - The redirect URI configured for the oauth client
  • ClientId - The Client ID (aka Application ID) of the OAuth client
  • RedirectUriIsFake - If set to true, the control will hide itself (visible=false) upon successfully retrieving an auth token. This exists due to the non-web nature of a .NET app -- there is not necessarially a webpage to redirect to. If a fake URL is used in the configuration (e.g. http://notarealserver/), setting this property to true prevents the user from seeing errors in the browser related to being unable to resolve the address. This setting defaults to false.

The following events may be useful for the consuming application:

  • ExceptionEncountered - Raised when an exception is encountered during an OAuth flow
  • Authenticated - Raised when an OAuth flow has completed and the auth token has been retrieved

In addition to the properties and events defined above, the properties, events, and methods of the underlying WebBrowser control are available.

Starting the Implicit Grant Flow

To start the implicit grant flow, invoke the BeginImplicitGrant() method. The Authorized event will be raised when the flow has completed.

Closing the Control

When finished with the control, simply dispose of it by calling its Dispose() method or by letting .NET garbage collect it.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 961 2/14/2022
2.1.0 6,001 7/18/2019
2.0.0 5,762 8/2/2018
1.2.0 4,133 7/22/2016
1.1.0 1,270 7/19/2016
1.0.0.1 1,629 2/24/2016

* Removed unused ClientSecret property