Reputation: 753
I have a middleware in my Startup.cs file which checks the users current authentication state. However, when debugging, it appears that even after a successful login the authentication remains false. I have searched for a solution, many of which provide solutions that not do work or do not apply to my project. I was wondering if anyone has come across this issue or could provide any insight into how I might resolve this issue. Thanks in advance
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthentication();
app.Use(async (context, next) =>
{
await next.Invoke();
if (context.User.Identity.IsAuthenticated)
{
var username = context.User.Identity.Name;
using (var dbContext = context.RequestServices.GetRequiredService<ApplicationDbContext>())
{
var user = dbContext.Users.Where(u => u.UserName == username).FirstOrDefault();
user.LastAccessed = DateTime.Now;
dbContext.Update(user);
dbContext.SaveChanges();
}
}
});
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
I feel like it has no idea if someone has logged in since the name does not show either
Upvotes: 2
Views: 2465
Reputation: 19961
As you see in the picture in your question, made a snippet here:
That means that Microsoft is looking for the name and role claim that is in the format of a URL, like the picture shows.
To tell Microsoft what the name of your claim is, then you need to add :
opt.TokenValidationParameters.RoleClaimType = "roles";
opt.TokenValidationParameters.NameClaimType = "name";
To complement this answer, I wrote a blog post that goes into more detail about this topic: Debugging OpenID Connect claim problems in ASP.NET Core
Upvotes: 0
Reputation: 20748
Probably not what OP is expecting but this may come in handy for anyone in the future stumbling upon this question. .NET expects ClaimsIdentity
to have AuthenticationType
property set to be considered Authenticated.
ClaimsIdentity.IsAuthenticated
true if the AuthenticationType property is not null or an empty string.
When I made a custom AuthenticationStateProvider
, I had to make sure to set it:
public class CustomAuthStateProvider : AuthenticationStateProvider
{
public async override Task<AuthenticationState> GetAuthenticationStateAsync()
{
string? authType = null;
// ...
// Without this, it won't be considered authenticated
authType = "Token";
var identity = new ClaimsIdentity(claims, authType);
var state = new AuthenticationState(new ClaimsPrincipal(identity));
return await Task.FromResult(state);
}
}
EDIT: I just took a closer look at OP's screenshot. Looks like it's happening to an API server code. I was fooled by the blazor-webassembly
tag.
Upvotes: 5
Reputation: 306
You could try to put the app.UseAuthentication();
before app.UseRouting();
like this :
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseIdentityServer();
app.UseAuthentication();
app.UseRouting();
Upvotes: 0