如何使用 Asp.Net 标识检查 Azure 表中是否存在记录并返回自定义响应



My Xamarin.Forms 移动应用使用 Web API 和 Asp.Net 标识来注册用户并将信息存储在名为 dbo 的默认 Azure 表中。AspNetUsers.我想在注册用户之前检查记录是否存在,如果用户已经存在,则返回相关消息(在这种情况下,电子邮件应该是唯一的(。

在我的网络 API 中:

    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Region = model.Region };
        IdentityResult result = await UserManager.CreateAsync(user, model.Password);
        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }
        return Ok();
    }

在我的Xamarin.Forms PCL中:

public class RegisterBindingModel
{
    public string Email { get; set; }
    public string Region { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
}
public async Task<bool> RegisterAsync (string email, string password, string confirmPassword, string firstName, string lastName, string region)
    {
        var client = new HttpClient();
        // create object to send to web api for registering
        var model = new RegisterBindingModel
        {
            Email = email,
            Password = password,
            ConfirmPassword = confirmPassword,
            FirstName = firstName,
            LastName = lastName,
            Region = region
        };
        var json = JsonConvert.SerializeObject(model);
        HttpContent content = new StringContent(json);
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        // add for API Key
        content.Headers.Add(AppConstants.API_KEY_NAME, AppConstants.API_KEY_VALUE);
        var response = await client.PostAsync(AppConstants.AZURE_WEB_API_REGISTER_URL, content);
        return response.IsSuccessStatusCode;
    }

粗略浏览一下UserManager的文档,就会发现FindByNameAsync方法。

感谢@Jason的帮助。最后很容易。这就是我解决它的方式:

    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Region = model.Region };
        var existingUser = await UserManager.FindByEmailAsync(user.Email);
        if (existingUser == null)
        {
            IdentityResult result = await UserManager.CreateAsync(user, model.Password);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }
            return Ok();
        }
        else
        {
            var errorModel = new
            {
                error = new
                {
                    code = 400,
                    message = "Email already exists"
                }
            };
            return Content(HttpStatusCode.BadRequest, errorModel);
        }         
    }

相关内容

  • 没有找到相关文章

最新更新