Auf dem Setup der Authentifizierung/Autorisierung festhaltenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Auf dem Setup der Authentifizierung/Autorisierung festhalten

Post by Anonymous »

Ich möchte die Authentifizierung und Autorisierung für mein Projekt einrichten. Ich habe ein wenig Erfahrung mit Identity Server, da die meisten meiner Clients sie verwenden, aber da es sich meistens eingerichtet hat, habe ich keine Erfahrung darin, es selbst zu richten.
Ich schaue auf die Dokumentationsproben, die Codemaze und alles andere, aber ich stecke fest. In der Dokumentation wird das Frontend zum Backend authentifiziert, in dem ich Benutzername und Passwort, 2FA, .... < /p>
Mein Projekt verwendet wird derzeit in .NET Aspire orchestriert und verwendet .NET 8.0.
Ich habe Duende bereits eingerichtet /> Für die Frontend gehe ich mit einer Blazor -Web -App oder Blazor Server und WASM. Diese Vorlage erstellt 2 Baugruppen - Client und Server. Da ich Benutzernamen und Passwörter möchte, habe ich mit dem Konto -Typ "einzelne Konten" gegangen, dies gibt mir alle Seiten, die ich möchte. Logik, die bereits in meinem Backend Identity Server API -Projekt befindet, hauptsächlich im ApplicationDBContext und Migrationen. Wie auch immer, ich kann meinen Frontend nicht ausführen, da ich einen Abhängigkeitsinjektionsfehler auf UserManager . >

Code: Select all

builder.Services.AddScoped();
builder.Services.AddScoped();
< /code>

Es kann den Dienst für den Typ 'microsoft.aspnetcore.Identity.iusSerStore nicht beheben1[Xyflux.Frontend.Data.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager< /code> 1 [xyflux.frontend.data.ApplicationUser] '< /p>
< /blockquote>

System.aggregateException:' Einige Dienste sind nicht In der Lage, konstruiert zu werden1[Xyflux.Frontend.Data.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1 [xyflux.frontend.data.ApplicationUser] ': Dienste für Typ' microsoft.aspnetcore.Identity.iusStore kann nicht behoben werden

Code: Select all

1[Xyflux.Frontend.Data.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager
1 [xyflux.frontend.data.ApplicationUser] '.) (Fehler beim Validieren des Dienstdeskriptor

Code: Select all

1[Xyflux.Frontend.Data.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager
1 [xyflux.frontend.data.ApplicationUser] ': Dienste für Typ' microsoft.aspnetcore.Identity.iusStore kann nicht behoben werden

Code: Select all

1[Xyflux.Frontend.Data.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager
1 [xyflux.frontend.data.ApplicationUser] '.) (Fehler beim Validieren des Dienstdeskriptors' servicetype: xyflux.frontend.components.account.idalityUserAccessor Lifetime: Scoped ImplementationType: xyflux.frontend.components.Identity.Identity. ': Service für Typ kann nicht behoben werden 'Microsoft.aspnetcore.Identity.iusStore

Code: Select all

1[Xyflux.Frontend.Data.ApplicationUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager
1 [xyflux.frontend.data.ApplicationUser] '.)'

und volles di
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Xyflux.Frontend.Components;
using Xyflux.Frontend.Components.Account;
using Xyflux.Frontend.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();

builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();

builder.Services.AddBff();

builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.Name = "BlazorApp.Cookie";
options.Cookie.HttpOnly = true;
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5001";
options.ClientId = "blazor-client";
options.ClientSecret = "your_client_secret";
options.ResponseType = "code";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("api1");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.ForwardSignIn = "";

});

builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});

builder.Services.AddSingleton();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();

app.UseStaticFiles();
app.UseAntiforgery();

app.MapRazorComponents()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(Xyflux.Frontend.Client._Imports).Assembly);

// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();

app.Run();
< /code>
Ich verwende auch das Framework duende.bff. sind?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post