3

I have a .NET Core 2.0 Web API. I am using Jwt authentication on it.

Whenever the application reaches this line, an exception is thrown:

var userClaims = await _userManager.GetClaimsAsync(user);

The exception is:

System.InvalidOperationException: Sequence contains more than one matching element

What is weird is that this wasn't happening before, it started happening when I upgraded to .NET Core 2.0 from 1.1.

The Seed method for the database is below:

public async Task Seed()
{
  var adminUserJ = await _userManager.FindByNameAsync("Ciwan");
  var regularUser = await _userManager.FindByNameAsync("John");

  if (adminUserJ == null)
  {
    if (!await _roleManager.RoleExistsAsync("Admin"))
    {
      var role = new IdentityRole("Admin");
      role.Claims.Add(new IdentityRoleClaim<string> { ClaimType = "IsAdmin", ClaimValue = "True" });
      await _roleManager.CreateAsync(role);
    }

    adminUserJ = new BbUser
    {
      UserName = "Ciwan",
      Email = "[email protected]"
    };

    var userResult = await _userManager.CreateAsync(adminUserJ, "Welcome123");
    var roleResult = await _userManager.AddToRoleAsync(adminUserJ, "Admin");
    var claimResult = await _userManager.AddClaimAsync(adminUserJ, new Claim("Points", "10"));

    if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
    {
      throw new InvalidOperationException("Failed to build user and roles");
    }
  }

  if (regularUser == null)
  {
    if (!await _roleManager.RoleExistsAsync("Regular"))
    {
      var role = new IdentityRole("Regular");
      role.Claims.Add(new IdentityRoleClaim<string> { ClaimType = "IsRegular", ClaimValue = "True" });
      await _roleManager.CreateAsync(role);
    }

    regularUser = new BbUser
    {
      UserName = "John",
      Email = "[email protected]"
    };

    var userResult = await _userManager.CreateAsync(regularUser, "BigWow321");
    var roleResult = await _userManager.AddToRoleAsync(regularUser, "Regular");
    var claimResult = await _userManager.AddClaimAsync(regularUser, new Claim("Points", "10"));

    if (!userResult.Succeeded || !roleResult.Succeeded || !claimResult.Succeeded)
    {
      throw new InvalidOperationException("Failed to build user and roles");
    }
  }

  _context.AddRange(GetListOfArtists(adminUserJ.Id, regularUser.Id));
  await _context.SaveChangesAsync();
}

I can't see anything wrong. I tried looking at the AspNetUserClaims table in the database, but all seems OK. I have 2 claims in there, one for each user.

This error happens when I attempt to log in a user, so the request arrives here:

[HttpPost("auth/token")]
public async Task<IActionResult> CreateToken([FromBody] CredentialsDto credentials)
{
  try
  {
    var user = await _userManager.FindByNameAsync(credentials.Username);
    if (user != null)
    {
      if (IsUserPasswordValid(credentials, user))
      {
        var claims = await GetClaimsAsync(user);
        var token = CreateNewJwtToken(claims);

        return Ok(new
        {
          token = new JwtSecurityTokenHandler().WriteToken(token),
          expiration = token.ValidTo
        });
      }
    }
  }
  catch (Exception exception)
  {
    Console.WriteLine(exception);
    throw;
  }
  return BadRequest("Failed to generate token!");
}

private async Task<IEnumerable<Claim>> GetClaimsAsync(BbUser user)
{
  var userClaims = await _userManager.GetClaimsAsync(user);
  return new[] {
          new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
          new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
  }.Union(userClaims);
}
18
  • can you shows that webapi Methods associated with the call to var userClaims = await GetClaimsAsync(user) Commented Dec 30, 2017 at 1:02
  • Thanks @mvermef, I've added that code. Commented Dec 30, 2017 at 1:22
  • are you rolling your own security are you using something like IdentityServer? Commented Dec 30, 2017 at 2:30
  • IdentityServer is what I am using Commented Dec 30, 2017 at 9:24
  • 2
    @mvermef It seems that Ciwan isn't using IdentityServer4 at all. There is no code where IdentityServer4 is configured.Check his other questions. I think the IdentityServer4 flag should be removed. Commented Dec 31, 2017 at 12:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.