Library that simplifies integration of OpenIdDict to a several lines of code.
It contain basic implementation of AuthenticationController
, mostly taken from OpenIddict Samples.
- Required endpoints to support 3rd party authentication (e.g. Google, Facebook, etc.)
- JWT access_token/refresh_token generation (i.e. default
/connect/token
endpoint) - Refresh token flow
- Authorization Code flow
- Resource Owner Password Flow (disabled by default, could be enabled via configuration)
EnableIdentityServerRefreshTokens()
option that eases the migration from IdentityServer (i.e., that RefreshTokens from IdentityServer will still work, ifPersistedGrants
table remains)app.UseOpenIdDictConversionMiddleware()
helps to support old clients when migrating from IdentityServer. It allows to do the following:- Remove non-existing scopes
- Remove header authorization (if client_id/client_secret are passed in Form parameters)
- Remove client_secret for public clients (otherwise OpenIdDict complains)
- Change name of form parameters (e.g.
userName
->username
)
Here's the vanilla js or react demo of Google/Facebook authentication using OpenIddict on backend. The page has several buttons to log in via different providers.
-
PREREQUISITE: it's implied, that openiddict is installed and configured in your project already (if it's not, head over to one of the samples).
-
Install nuget to add the library to your project.
dotnet add package Shaddix.OpenIddict.ExternalAuthentication
-
Create you own
AuthorizationController
by inheriting fromOpenIdAuthorizationControllerBase
. This could look like:public class AuthorizationController : OpenIdAuthorizationControllerBase<IdentityUser, string> { public AuthorizationController(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager, IOpenIddictClientConfigurationProvider clientConfigurationProvider) : base(signInManager, userManager, clientConfigurationProvider) { } }
-
Override some functions (e.g.
CreateNewUser
orGetClaims
) if you want to customize user creation behavior or provide more claims. -
From
Configure
function inStartup.cs
add the following calls (in addition to standard OpenIddict setup):services .AddOpenIddict() .AddOpenIddictConfigurations(Configuration) .AddDefaultAuthorizationController()
You could customize default authorization controller configuration (or even default OpenIddict configuration) by doing:
.AddDefaultAuthorizationController(options => options.DisableRefreshTokenFlow())
or
.AddDefaultAuthorizationController(options => options.OpenIddictServerBuilder.AllowNoneFlow())
-
Add external auth providers (i.e.
.AddAuthentication().AddGoogle()
,.AddFacebook()
, etc.). Follow instructions on how to set up applications on OAuth provider side.
You could also take a look at OpenIddictExternalAuthentication.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).
- Use some proven openid client library (I personally recommend oidc-client-ts).
- Use standard auth code flow according to the library instructions, pointing to standard Authorize endpoint and passing
?provider=Google
as a query parameter (i.e. authorization endpoint should look like/connect/authorize?provider=Google
). - You could check example implementation in plain-js or React
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)
- Error in browser: "The specified 'redirect_uri' is not valid for this client application."
- Check
OpenIddictApplications
table and verify thatRedirectUris
field contains the URI you are redirecting to. - If URI is not there, check
RedirectUris
inappsettings.json
for the respective application. - If problematic URI is a relative one, make sure that you called
options.SetPublicUrl()
with correct URL inAddDefaultAuthorizationController
configuration callback.
- Check