Search code examples
c#apiidentityroles

Registering user with specific email to specific role


I'm trying to create an register method that I have in controller:

[HttpPost("register")]
    public async Task<IActionResult> RegisterUser(UserForRegisterDto userForRegisterDto)
    {
        var userToCreate = _mapper.Map<User>(userForRegisterDto);
        var result = await _userManager.CreateAsync(userToCreate, userForRegisterDto.Password);

        if (result.Succeeded)
        {
            return Ok();
        }

        return BadRequest();
    }

I'm trying to add role with specific email address (for example: every email service with '*@example.com gets' role 'Member').

I know that is operator like but I have very little knowledge to use with it.

Is there any other way than using that operator?


Solution

  • You can use EndsWith:

    var role = "None";
    if (userForRegisterDto.EmailAddress.EndsWith("@example.com"))
    {
       role = "Member";
    }
    else if (userForRegisterDto.EmailAddress.EndsWith("@myownsite.com"))
    {
       role = "Admin";
    }