Browse Source

Update the samples to register the OpenIddict entity sets directly from Startup.cs

pull/295/head
Kévin Chalet 10 years ago
parent
commit
55e83e285d
  1. 54
      README.md
  2. 6
      samples/Mvc.Server/Models/ApplicationDbContext.cs
  3. 11
      samples/Mvc.Server/Startup.cs

54
README.md

@ -72,8 +72,15 @@ To use OpenIddict, you need to:
public void ConfigureServices(IServiceCollection services) { public void ConfigureServices(IServiceCollection services) {
services.AddMvc(); services.AddMvc();
services.AddDbContext<ApplicationDbContext>(options => services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); // Configure the context to use Microsoft SQL Server.
options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]);
// Register the entity sets needed by OpenIddict.
// Note: use the generic overload if you need
// to replace the default OpenIddict entities.
options.UseOpenIddict();
});
// Register the Identity services. // Register the Identity services.
services.AddIdentity<ApplicationUser, IdentityRole>() services.AddIdentity<ApplicationUser, IdentityRole>()
@ -129,43 +136,30 @@ public void Configure(IApplicationBuilder app) {
- **Update your Entity Framework context registration to register the OpenIddict entities**: - **Update your Entity Framework context registration to register the OpenIddict entities**:
```csharp ```csharp
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { services.AddDbContext<ApplicationDbContext>(options => {
public ApplicationDbContext(DbContextOptions options) // Configure the context to use Microsoft SQL Server.
: base(options) { } options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]);
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. // Register the entity sets needed by OpenIddict.
// For example, you can rename the ASP.NET Identity table names and more. // Note: use the generic overload if you need
// Add your customizations after calling base.OnModelCreating(builder); // to replace the default OpenIddict entities.
} options.UseOpenIddict();
} });
``` ```
> **Note:** if you change the default entity primary key (e.g. to `int` or `Guid` instead of `string`), make sure to use the `services.AddOpenIddict<TKey>()`/`builder.UseOpenIddict<TKey>()` overloads 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 `services.AddOpenIddict()` extension accepting a `TKey` generic argument and use the generic `options.UseOpenIddict<TKey>()` overload:
```csharp ```csharp
services.AddOpenIddict<Guid>() services.AddOpenIddict<Guid>()
.AddEntityFrameworkCoreStores<ApplicationDbContext>() .AddEntityFrameworkCoreStores<ApplicationDbContext>()
```
```csharp services.AddDbContext<ApplicationDbContext>(options => {
protected override void OnModelCreating(ModelBuilder builder) { // Configure the context to use Microsoft SQL Server.
// Register the entity sets needed by OpenIddict. options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]);
builder.UseOpenIddict<Guid>();
base.OnModelCreating(builder); options.UseOpenIddict<Guid>();
});
// 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**: - **Create your own authorization controller**:

6
samples/Mvc.Server/Models/ApplicationDbContext.cs

@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Mvc.Server.Models { namespace Mvc.Server.Models {
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
@ -8,11 +7,6 @@ namespace Mvc.Server.Models {
: base(options) { } : base(options) { }
protected override void OnModelCreating(ModelBuilder builder) { 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); base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed. // Customize the ASP.NET Identity model and override the defaults if needed.

11
samples/Mvc.Server/Startup.cs

@ -21,8 +21,15 @@ namespace Mvc.Server {
services.AddMvc(); services.AddMvc();
services.AddDbContext<ApplicationDbContext>(options => services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"])); // Configure the context to use Microsoft SQL Server.
options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]);
// Register the entity sets needed by OpenIddict.
// Note: use the generic overload if you need
// to replace the default OpenIddict entities.
options.UseOpenIddict();
});
// Register the Identity services. // Register the Identity services.
services.AddIdentity<ApplicationUser, IdentityRole>() services.AddIdentity<ApplicationUser, IdentityRole>()

Loading…
Cancel
Save