In your Program.cs file ensure you have:
app.UseAuthentication();
app.UseAuthorization();
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have created the project on blazor Server-Side and It will authenticating from another project of Identity Server 4.
If I visit the website it redirect me to authenticate (IS4 project) and after login it will get back to website but the I cannot see the who is authenticated. The AutheticationStateProvider is null and also IHttpContextAccessor also null/empty.
index.razor (blazor Server-Side)
@inject AuthenticationStateProvider auth
@inject IHttpContextAccessor httpContextAccessor
var user_auth = await auth.GetAuthenticationStateAsync();
string Name = user_auth.User.Identity.Name;
string AccName = "acc" + httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(a=> a.Type == "email").Value;
What is the issue?
Thanks
In your Program.cs file ensure you have:
app.UseAuthentication();
app.UseAuthorization();
this my program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:7065/") });
builder.Services.AddBlazoredLocalStorage(config =>
config.JsonSerializerOptions.WriteIndented = true);
builder.Services.AddBlazoredSessionStorage(config =>
config.JsonSerializerOptions.WriteIndented = true);
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme,
options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.SignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.Authority = builder. Configuration["OIDC:Authority"];
options.ClientId = builder. Configuration["OIDC:ClientId"];
options.ClientSecret = builder.Configuration["OIDC:ClientSecret"];
options.CallbackPath = builder.Configuration["OIDC:RedirectUri"];
options.SignedOutCallbackPath = builder.Configuration["OIDC:PostLogoutRedirectUri"];
options.ResponseType = "code";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "email"
};
options.Events = new OpenIdConnectEvents
{
OnAccessDenied = context =>
{
context.HandleResponse();
context.Response.Redirect("/");
return Task.CompletedTask;
}
};
});
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddHttpContextAccessor();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<IWorksService, WorksService>();
var app = builder. Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();