Integrating ASP.NET Core Identity
Xperience provides an integration API that allows you to work with the system's membership data on websites presented by ASP.NET Core applications. The API is available in the Kentico.Membership namespace, which is provided as part of the Xperience.AspNetCore.WebApp integration package.
You can set up the following scenarios:
- Allow visitors to sign in with Xperience user accounts
- Allow users to register new accounts from the ASP.NET Core site (the user data is stored in the shared Xperience database)
- Allow users to reset their passwords
- Authorize actions based on Xperience roles
- Use external services for authentication
The membership integration is based on ASP.NET Core Identity. As a result, you can work with user data through the standard approaches that you would use in any ASP.NET Core application.
Download and examine the LearningKit project for a sample implementation of the Xperience membership integration.
Integrating Xperience Identity into your project
Before you can start working with Xperience membership data in your ASP.NET Core application, you need to register the required API:
- Open your ASP.NET Core project in Visual Studio.
- Install the Microsoft.AspNetCore.Identity NuGet package.
Modify the startup file of your project (Startup.cs by default).
In the ConfigureServices method, add types and services required to work with Xperience Identity:
using Microsoft.AspNetCore.Identity; using CMS.Helpers; using Kentico.Membership;
private const string AUTHENTICATION_COOKIE_NAME = "identity.authentication"; public void ConfigureServices(IServiceCollection services) { ... // Adds Xperience services required by the system's Identity implementation services.AddScoped<IPasswordHasher<ApplicationUser>, Kentico.Membership.PasswordHasher<ApplicationUser>>(); services.AddScoped<IMessageService, MessageService>(); services.AddApplicationIdentity<ApplicationUser, ApplicationRole>() // Adds token providers used to generate tokens for email confirmations, password resets, etc. .AddApplicationDefaultTokenProviders() // Adds an implementation of the UserStore for working with Xperience user objects .AddUserStore<ApplicationUserStore<ApplicationUser>>() // Adds an implementation of the RoleStore used for working with Xperience roles .AddRoleStore<ApplicationRoleStore<ApplicationRole>>() // Adds an implementation of the UserManager for Xperience membership .AddUserManager<ApplicationUserManager<ApplicationUser>>() // Adds the default implementation of the SignInManger .AddSignInManager<SignInManager<ApplicationUser>>(); // Adds authentication and authorization services provided by the framework services.AddAuthentication(); services.AddAuthorization(); // Configures the application's authentication cookie services.ConfigureApplicationCookie(c => { c.LoginPath = new PathString("/"); c.ExpireTimeSpan = TimeSpan.FromDays(14); c.SlidingExpiration = true; c.Cookie.Name = AUTHENTICATION_COOKIE_NAME; }); // Registers the authentication cookie in Xperience with the 'Essential' cookie level // Ensures that the cookie is preserved when changing a visitor's allowed cookie level below 'Visitor' CookieHelper.RegisterCookie(AUTHENTICATION_COOKIE_NAME, CookieLevel.Essential); ... }
In the Configure method, register the corresponding middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); ... }
Registering authentication cookies
We strongly recommend registering all authentication cookies used on your website with an appropriate cookie level (Essential when working with the default cookie level values).
Otherwise you may encounter problems with the cookies being cleared after adjusting the allowed cookie level for visitors (typically when visitors do not consent with tracking). Changes of the allowed cookie level automatically remove all cookies above the given level. Any unregistered cookies are processed with the Visitor level, which is usually too high for basic authentication cookies.
To register a cookie, call the CookieHelper.RegisterCookie method (available in the CMS.Helpers namespace of the Xperience API) in your application's startup code. You can access the default level values in the CookieLevel enumeration.
The Xperience Identity implementation is now registered and you can work with the Kentico.Membership API in your application's code. You can now implement the following features:
Configuring the Identity integration
The configuration of the Identity classes provided by Xperience (ApplicationUserManager, ApplicationUserStore, etc.) reflects the Security & Membership and Passwords settings configured via the Settings application in the administration interface. To ensure these settings do not interfere with existing Identity functionality, the system maps certain settings to the Identity configuration.
The following table details the mapping of all Xperience settings to ASP.NET Core Identity configuration options:
Settings | Identity configuration option | Notes |
---|---|---|
Require unique user emails | IdentityOptions.User.RequireUniqueEmail | These settings are mapped as part of the AddApplicationIdentity call before the user-provided Identity configuration is applied. |
Registration requires administrator's approval | IdentityOptions.SignIn.RequireConfirmedAccount | |
Reset password interval | DataProtectionTokenProviderOptions.TokenLifespan | This configuration is set during the AddApplicationDefaultTokenProviders call. If the reset interval is set to 0, the password reset token expiration defaults to 24 hours. |
Was this page helpful?