-
Notifications
You must be signed in to change notification settings - Fork 3
Getting started
Using the package manager console in visual studio run "Install-Package PaulMiami.AspNetCore.Identity.Authenticator.EntityFrameworkCore"
or
By adding the package directly to your "project.json" file:
"dependencies": {
...
"PaulMiami.AspNetCore.Identity.Authenticator.EntityFrameworkCore": "1.0.0"
...
}
and run "dotnet restore" in the console.
Instead of inheriting from IdentityUser or IdentityUser<TKey> you "ApplicationUser" class should inherit from "AuthenticatorUser" or "AuthenticatorUser<TKey>".
Modify your "ApplicationUser" DbContext with the appropriate base class definition.
Example if your "ApplicationUser" inherits from "AuthenticatorUser":
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string>
{
...
}
In your "Startup" class add the "AuthenticatorService" and "AuthenticatorUserStore" in the "ConfigureServices" method:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddAuthenticatorEntityFrameworkStore<ApplicationDbContext>()
.AddAuthenticator(c =>
{
c.Issuer = "YourCompanyName";
});
services.AddMvc();
}
Note: call "AddAuthenticatorEntityFrameworkStore" and "AddAuthenticator" always after "AddEntityFrameworkStores", because it is replacing the default UserStore and UserManager.
- Generate a migration script, run:
dotnet ef migrations add Authenticator
- Update your database, run:
dotnet ef database update
Replace the base UserManager with "AuthenticatorUserManager", it will give you 5 additional methods to deal with managing user's authenticator.
- "GetAuthenticatorEnabledAsync": Checks is the user has an authenticator enabled on his account.
- "GetAuthenticatorParamsAsync": Gets the authenticator parameters for this user.
- "EnableAuthenticatorAsync": Enables the given authenticator for this user (throws if the user already has an authenticator enabled).
- "DisableAuthenticatorAsync": Disables the current authenticator for this user.
- "CreateAuthenticatorAsync": Generate a new authenticator (throws if the user already has an authenticator enabled).
- https://github.com/PaulMiami/Authenticator/tree/master/sample/TestWebApp for a simple example of the core library .
- https://github.com/PaulMiami/Authenticator/tree/develop/sample/TestWebAppIdentity for an example with Identity using EntityFramework.