diff --git a/README.md b/README.md index cd4d56d2..58dc2be5 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ To use OpenIddict, you need to: "dependencies": { "AspNet.Security.OAuth.Validation": "1.0.0-alpha2-final", "OpenIddict": "1.0.0-*", + "OpenIddict.EntityFrameworkCore": "1.0.0-*", "OpenIddict.Mvc": "1.0.0-*" } ``` @@ -71,15 +72,18 @@ public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDbContext(options => - options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); + options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); // Register the Identity services. services.AddIdentity() - .AddEntityFrameworkStores() + .AddEntityFrameworkCoreStores() .AddDefaultTokenProviders(); - // Register the OpenIddict services, including the default Entity Framework stores. - services.AddOpenIddict() + // Register the OpenIddict services. + services.AddOpenIddict() + // Register the Entity Framework stores. + .AddEntityFrameworkCoreStores() + // Register the ASP.NET Core MVC binder used by OpenIddict. // Note: if you don't call this method, you won't be able to // bind OpenIdConnectRequest or OpenIdConnectResponse parameters. @@ -121,20 +125,46 @@ public void Configure(IApplicationBuilder app) { > **Note:** `UseOpenIddict()` must be registered ***after*** `app.UseIdentity()` and the external social providers. - - **Update your Entity Framework context to inherit from `OpenIddictDbContext`**: + - **Update your Entity Framework context registration to register the OpenIddict entities**: ```csharp -public class ApplicationDbContext : OpenIddictDbContext { +public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) - : base(options) { + : base(options) { } + + protected override void OnModelCreating(ModelBuilder builder) { + // Register the entity sets needed by OpenIddict. + // Note: use the generic overload if you need + // to replace the default OpenIddict entities. + builder.UseOpenIddict(); + + base.OnModelCreating(builder); + + // Customize the ASP.NET Identity model and override the defaults if needed. + // For example, you can rename the ASP.NET Identity table names and more. + // Add your customizations after calling base.OnModelCreating(builder); } } ``` -> **Note:** if you change the default entity primary key (e.g. to `int` or `Guid` instead of `string`), make sure to register your Entity Framework context using the overload accepting a `TKey` generic argument: +> **Note:** if you change the default entity primary key (e.g. to `int` or `Guid` instead of `string`), make sure to use the `AddEntityFrameworkCoreStores` overload accepting a `TKey` generic argument and use the generic `builder.UseOpenIddict()` overload: + +```csharp +services.AddOpenIddict() + .AddEntityFrameworkCoreStores() +``` ```csharp -services.AddOpenIddict() +protected override void OnModelCreating(ModelBuilder builder) { + // Register the entity sets needed by OpenIddict. + builder.UseOpenIddict(); + + base.OnModelCreating(builder); + + // Customize the ASP.NET Identity model and override the defaults if needed. + // For example, you can rename the ASP.NET Identity table names and more. + // Add your customizations after calling base.OnModelCreating(builder); +} ``` - **Create your own authorization controller**: @@ -148,8 +178,11 @@ The **Mvc.Server sample comes with an [`AuthorizationController` that supports b ```csharp public void ConfigureServices(IServiceCollection services) { - // Register the OpenIddict services, including the default Entity Framework stores. - services.AddOpenIddict() + // Register the OpenIddict services. + services.AddOpenIddict() + // Register the Entity Framework stores. + .AddEntityFrameworkCoreStores() + // Register the ASP.NET Core MVC binder used by OpenIddict. // Note: if you don't call this method, you won't be able to // bind OpenIdConnectRequest or OpenIdConnectResponse parameters. @@ -179,8 +212,10 @@ using (var context = new ApplicationDbContext( app.ApplicationServices.GetRequiredService>())) { context.Database.EnsureCreated(); - if (!context.Applications.Any()) { - context.Applications.Add(new OpenIddictApplication { + var applications = context.Set(); + + if (!applications.Any()) { + applications.Add(new OpenIddictApplication { // Assign a unique identifier to your client app: Id = "48BF1BC3-CE01-4787-BBF2-0426EAD21342",