Last active
May 4, 2024 09:11
-
-
Save KerryRitter/9087fa12c93fffba7f27666b6cc1d24e to your computer and use it in GitHub Desktop.
OpenIddict Startup Example (with postgres and custom password rules)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.EntityFrameworkCore.Infrastructure; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | |
using Microsoft.EntityFrameworkCore; | |
namespace MyApp.Api | |
{ | |
public class Startup | |
{ | |
public IConfigurationRoot Configuration { get; } | |
public Startup(IHostingEnvironment env) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) | |
.AddEnvironmentVariables(); | |
Configuration = builder.Build(); | |
} | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddCors(); // <- ADD THIS | |
services.AddMvc(); | |
services.AddEntityFramework() | |
.AddEntityFrameworkNpgsql() | |
.AddDbContext<ApplicationDbContext>(options => | |
options.UseNpgsql(Configuration["Data:DefaultConnection"])); | |
services.AddIdentity<ApplicationUser, IdentityRole>(o => { | |
o.Password.RequireDigit = false; | |
o.Password.RequireLowercase = false; | |
o.Password.RequireUppercase = false; | |
o.Password.RequireNonAlphanumeric = false; | |
o.Password.RequiredLength = 6; | |
}) | |
.AddEntityFrameworkStores<ApplicationDbContext>() | |
.AddDefaultTokenProviders() | |
.AddOpenIddict(); // <- ADD THIS | |
services.AddSingleton(Configuration); | |
services.AddSingleton<IContextFactory, ContextFactory>(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
loggerFactory.AddDebug(); | |
app.UseCors(builder => // <- ADD THIS | |
builder.AllowAnyHeader() | |
.AllowAnyMethod() | |
.AllowAnyOrigin() | |
); | |
app.UseOAuthValidation(); // <- ADD THIS | |
app.UseMvc(); | |
app.UseOpenIddict(); // <- ADD THIS | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nowadays, only thing you need to is to take any of the openiddict examples (in the examples repo) is change:
options.UseSqlServer(...);
to
options.UseNpgsql(...)
then change your connectionstring to a valid postgresql one and you're done.