You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.5 KiB

10 months ago
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using OpenIddict.Validation.AspNetCore;
using System.Text;
namespace HuiXin.Identity.OpenIddict
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddCors();
builder.Services.AddControllers();
builder.Services
.AddAuthentication(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme)
.AddCookie();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
//options.UseSqlite("DataSource=:memory:");
options.UseMySQL("server=8.134.236.110;port=13306;database=openiddict;user=test;password=test")
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
// Register the entity sets needed by OpenIddict.
// Note: use the generic overload if you need to replace the default OpenIddict entities.
options.UseOpenIddict();
});
builder.Services.AddIdentity<UserInfo, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
builder.Services
.AddOpenIddict()
// Register the OpenIddict core components.
.AddCore(options =>
{
// Configure OpenIddict to use the Entity Framework Core stores and models.
// Note: call ReplaceDefaultEntities() to replace the default entities.
options.UseEntityFrameworkCore()
.UseDbContext<ApplicationDbContext>();
})
// Register the OpenIddict server components.
.AddServer(options =>
{
options.SetAccessTokenLifetime(TimeSpan.FromMinutes(5));
options.SetIdentityTokenLifetime(TimeSpan.FromMinutes(5));
options.SetRefreshTokenLifetime(TimeSpan.FromDays(365 * 100));
// Enable the token endpoint.
options.SetTokenEndpointUris("auth/connect/token");
options.SetAuthorizationEndpointUris("auth/connect/authorize");
options.SetUserinfoEndpointUris("auth/connect/userinfo");
options.SetLogoutEndpointUris("auth/connect/logout");
// Enable the client credentials flow.
options.AllowClientCredentialsFlow();
options.AllowAuthorizationCodeFlow();
options.AllowPasswordFlow();
options.AllowRefreshTokenFlow();
// Register the signing and encryption credentials.
options.AddEncryptionKey(new SymmetricSecurityKey(Convert.FromBase64String("GcTdqSZdpRxBtdtgwvDHBzS427VGTQzbM+JD1CBbUZY=")));
options.AddDevelopmentSigningCertificate();
// Register the ASP.NET Core host and configure the ASP.NET Core options.
options.UseAspNetCore()
.EnableTokenEndpointPassthrough()
.EnableAuthorizationEndpointPassthrough()
.EnableUserinfoEndpointPassthrough()
.EnableLogoutEndpointPassthrough()
.DisableTransportSecurityRequirement();
options.IgnoreEndpointPermissions();
options.IgnoreGrantTypePermissions();
options.IgnoreScopePermissions();
options.IgnoreResponseTypePermissions();
})
// Register the OpenIddict validation components.
.AddValidation(options =>
{
// Import the configuration from the local OpenIddict server instance.
options.UseLocalServer();
// Register the ASP.NET Core host.
options.UseAspNetCore();
});
builder.Services.AddHostedService<Worker>();
var app = builder.Build();
// Configure the HTTP request pipeline.
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}