IdentityOAuthSpaExtensions 0.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package IdentityOAuthSpaExtensions --version 0.1.2
NuGet\Install-Package IdentityOAuthSpaExtensions -Version 0.1.2
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="IdentityOAuthSpaExtensions" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IdentityOAuthSpaExtensions --version 0.1.2
#r "nuget: IdentityOAuthSpaExtensions, 0.1.2"
#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 IdentityOAuthSpaExtensions as a Cake Addin
#addin nuget:?package=IdentityOAuthSpaExtensions&version=0.1.2

// Install IdentityOAuthSpaExtensions as a Cake Tool
#tool nuget:?package=IdentityOAuthSpaExtensions&version=0.1.2

IdentityOAuthSpaExtensions

.Net Core library that allows easy integration of external OAuth providers into your SPA. It has even more perks if you use IdentityServer.

What you can do with this library?

  • On SPA side you could receive AuthCode from OAuth provider (Authorization Code Flow)
  • On backend you could verify AuthCode (passed from your SPA) and get user information from oAuth provider
  • If you're using IdentityServer, you could plug-in an extension grant that will allow you to issue your own JWT tokens in exchange for AuthCode (and optionally create new users).

Goal

The project goal is to allow easy integration of external OAuth providers (e.g. Google, Facebook, etc.) into your SPA applications (React, Angular, plain-old-js, whatever), with the minimum amount of needed code. This is a backend library, that integrates with Asp.Net Core 2.2+. The library is kept minimal, as we reuse all official and non-official authentication providers (i.e. library doesn't need to be updated when those external providers change).

How to

Just install nuget to add the library to your project.

You could also take a look at IdentityOAuthSpaExtensions.Example for example usage (keep in mind, that there are hardcoded ClientId/ClientSecret for FB and Google within Example app. They are for demo purposes and everyone can use them, so beware).

Backend

From ConfigureServices call services.ConfigureExternalAuth(Configuration).

That's it.

After that you will be able to request AuthCode from SPA (instructions below), and manually verify AuthCode on backend: this.HttpContext.RequestServices.GetService<ExternalAuthService>().GetExternalUserId(providerName, authCode) or this.HttpContext.RequestServices.GetService<ExternalAuthService>().GetExternalUserInfo(providerName, authCode)

Frontend

To get AuthCode:

  • Create oAuthCode handlers, e.g.
      function externalAuthSuccess(provider, code) {
          alert(`Provider: ${provider}, code: ${code}`);
      }
      function externalAuthError(provider, error, errorDescription) {
          alert(`Provider: ${provider}, error: ${error}, ${errorDescription}`);
      }
    
  • Subscribe to messages on a window: window.addEventListener("message", this.oAuthCodeReceived, false); and provide oAuthCodeReceived implementation like:
        if (message.data) {
            let data = JSON.parse(message.data);
            if (data.type === 'oauth-result') {
                if (data.code) {
                    externalAuthSuccess(data.provider, data.code);
                } else {
                    externalAuthError(data.provider, data.error, data.errorDescription);
                }
            }
        }
    }```
    
    
  • Open authentication dialog in new window pointing to http://YOUR_BACKEND_HOST/external-auth/challenge?provider=${provider}. E.g.: ```window.open(${window.location.protocol}//${window.location.hostname}:${window.location.port}/external-auth/challenge?provider=${provider}`, undefined, 'width=800,height=600');```
  • When authentication succeeds/errors, your callback functions (externalAuthSuccess/externalAuthError) will be called.

To authenticate (get access_token) using IdentityServer

  • Get AuthCode (see above)
  • Call /connect/token?provider=${provider}&code=${code}&grant=external.

External user storage

We use standard Asp.Net Identity mechanism to store external logins (namely, AspNetUserLogins table). To find a user by external OAuth id you need to use _userManager.FindByLoginAsync(providerName, externalUserId)

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
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
1.0.7 449 11/13/2021
1.0.6 420 4/10/2021
1.0.5 2,084 3/14/2021
1.0.4 392 3/14/2021
1.0.3 403 3/14/2021
1.0.2 402 3/14/2021
1.0.1 354 3/13/2021
0.3.6 340 2/11/2021
0.3.5 342 1/25/2021
0.3.4 310 1/25/2021
0.3.3 377 11/23/2020
0.3.2 346 11/12/2020
0.2.3 438 7/16/2020
0.2.2 446 7/16/2020
0.2.1 431 7/11/2020
0.1.9 648 3/4/2019
0.1.8 664 2/24/2019
0.1.7 647 2/22/2019
0.1.6 631 2/21/2019
0.1.5 681 2/19/2019
0.1.3 660 2/17/2019
0.1.2 663 2/16/2019