ASPNET authentication works but value for user.Identity.Name is null

perfect code 271 Reputation points
2023-01-27T14:29:56.4566667+00:00

I get a null value for user.Identity.Name, but user.Identity.IsAuthenticated returns the correct value. I get my claim values too.

I have only found a few posts about user.Identity.Name is null, but nothing has solved it.

I have a simple login form:

                <form action="cookie" method="post">
                    <label>Email</label>
                    <input type="text" name="email" />
                    <label>Password</label>
                    <input type="password" name="password" />
                    <input class="btn btn-primary" type="submit" />
                </form>

In Program.cs I have:

builder.Services.AddAuthentication("Cookies")
    .AddCookie(options =>
    {
        options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
        options.SlidingExpiration = true;
        options.AccessDeniedPath = "/Forbidden/";
    });
.
.
app.MapControllers(); 
app.UseAuthentication();
app.UseAuthorization();

My controller looks like this:

   [Route("/[controller]")]
    [ApiController]
    public class CookieController : ControllerBase
    {
        [HttpPost]
        public async Task<ActionResult> Login([FromForm] string email)
        {
            ClaimsIdentity claimsIdentity = new ClaimsIdentity(new List<Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, email),
                new Claim(ClaimTypes.Email, email)
            }, "auth");
            ClaimsPrincipal claims = new ClaimsPrincipal(claimsIdentity);
            await HttpContext.SignInAsync(claims);
            return Redirect("/");
        }
    }

And in the blazor page I have:

        [CascadingParameter] 
        Task<AuthenticationState> authenticationStateTask { get; set; }

.
.
                    var authState = await authenticationStateTask;
                    var user = authState.User;

                    if (user.Identity.IsAuthenticated)
                    {
                        string h = 
                        //authMessage = $"{user.Identity.Name} is authenticated.";
                    }
                    else
                    {
                        //authMessage = "The user is NOT authenticated.";
                    }

As I said, the authentication works, but I get a null value here for user.Identity.Name.

Is the error somewhere with me, or has Microsoft changed something?

I am using NET 7, Blazor Server

Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,195 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,398 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2023-01-27T15:14:47.5966667+00:00

    I believe the name claim is

    new Claim(ClaimTypes.Name, user.Email),
    
    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful