Skip to content

Getting started

Paul Biccherai edited this page Jul 23, 2016 · 6 revisions

Getting started

Install the package

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.

Setup Your ApplicationUser model

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>
    {
        ...
    }

Configure the service

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.

Update your database

  1. Generate a migration script, run:
dotnet ef migrations add Authenticator
  1. Update your database, run:
dotnet ef database update

Use in your Controller

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).

Examples

Clone this wiki locally