первый проект, IdentityServer, действует как сервер для аутентификации и авторизации пользователей.
Второй проект действует как клиент и используется для доступа к ресурсам.
В случае, если клиент не аутентифицирован, он перенаправляется на URI аутентификации первого проекта.
Ошибка следующая:
ошибка: valid_request
error_description: указанный redirect_uri недопустим для этого клиентского приложения.
error_uri: https://documentation.openiddict.com/errors/ID2043
Пример находится по этому URL p>
https://dev.to/isaacojeda/aspnet-core-s ... nnect-59kh
Файловая программа .cs первого проекта:
Код: Выделить всё
using System;
using IdentityOpenID.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using OpenIddict.Abstractions;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext(options =>
{
options.UseSqlServer(connectionString);
options.UseOpenIddict();
});
ConfigureFlowOpenID(builder);
builder.Services
.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores();
builder.Services.AddRazorPages();
builder.Services.AddControllers();
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
await SeedDefaultClients();
app.Run();
void ConfigureFlowOpenID(WebApplicationBuilder builder)
{
builder.Services
.AddOpenIddict()
.AddCore(options => {
options.UseEntityFrameworkCore()
.UseDbContext();
})
.AddServer(options => {
options.AllowClientCredentialsFlow()
.AllowAuthorizationCodeFlow()
.RequireProofKeyForCodeExchange()
.AllowRefreshTokenFlow();
options.SetTokenEndpointUris("/connect/token")
.SetAuthorizationEndpointUris("/connect/authorize")
.SetUserinfoEndpointUris("/connect /userinfo");
.AddEphemeralEncryptionKey()
.AddEphemeralSigningKey()
.DisableAccessTokenEncryption();
options.RegisterScopes("api");
options.RegisterScopes("profile");
options.UseAspNetCore()
.EnableTokenEndpointPassthrough()
.EnableAuthorizationEndpointPassthrough()
.EnableUserinfoEndpointPassthrough();
});
}
async Task SeedDefaultClients()
{
using var scope = app.Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService();
var manager = scope.ServiceProvider.GetRequiredService();
await context.Database.EnsureCreatedAsync();
var client = await manager.FindByClientIdAsync("clientwebapp");
if (client is null)
{
await manager.CreateAsync(new OpenIddictApplicationDescriptor {
ClientId = "clientwebapp",
ClientSecret = "client-web-app-secret",
DisplayName = "ClientWebApp",
RedirectUris = { new Uri("https://localhost:7006/signin-oidc") },
Permissions = { OpenIddictConstants.Permissions.Endpoints.Authorization,
Constants.Permissions.Endpoints.Token,
OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode,
OpenIddictConstants.Permissions.GrantTypes.ClientCredentials,
OpenIddictConstants.Permissions.GrantTypes.RefreshToken,
OpenIddictConstants.Permissions.Prefixes.Scope + "api", OpenIddictConstants.Permissions.Prefixes.Scope + "profile", OpenIddictConstants.Permissions.ResponseTypes.Code
}
});
}
}
Код: Выделить всё
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies", options =>
{
options.Cookie.Name = ".ClientWebAppAuth";
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:7281";
options.ClientId = "clientwebapp";
options.ClientSecret = "client-web-app-secret";
options.ResponseType = OpenIdConnectResponseType.Code;
options.CallbackPath = "/signin-oidc";
options.Scope.Add("api");
options.Scope.Add("openid");
options.Scope.Add("profile");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.TokenValidationParameters.NameClaimType = "name";
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages()
.RequireAuthorization();
app.Run();
Подробнее здесь: https://stackoverflow.com/questions/790 ... entication
Мобильная версия