Skip to content

Instantly share code, notes, and snippets.

@KerryRitter
Last active January 6, 2023 05:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save KerryRitter/9087fa12c93fffba7f27666b6cc1d24e to your computer and use it in GitHub Desktop.
Save KerryRitter/9087fa12c93fffba7f27666b6cc1d24e to your computer and use it in GitHub Desktop.
OpenIddict Startup Example (with postgres and custom password rules)
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
}
}
}
@hegelstad
Copy link

Questions:

  • What is added exactly to use Sqlite for development.
  • Why does the tutorial differ from the offical one on OpenIddict github pages.
  • What does ApplicationDbContext look like, is it unchanged?

@elgerm
Copy link

elgerm commented Jan 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment