Exemplo n.º 1
0
        public async Task <JsonResult> Register([System.Web.Http.FromBody] eCommerceUser model)
        {
            JsonResult       jsonResult = new JsonResult();
            eCommerceContext context    = new eCommerceContext();

            var fullName = model.FirstName + " " + model.MiddleName + " " + model.LastName;

            if (!ModelState.IsValid)
            {
                jsonResult.Data = new { Success = false, Messages = string.Format("ModelState is not valid") };

                return(jsonResult);
            }
            eCommerceUser result = await userApiService.GetUser(model.UserName, model.Email);


            var UserManager = new UserManager <eCommerceUser>(new UserStore <eCommerceUser>(context));

            if (result == null)
            {
                var NewUser = new eCommerceUser
                {
                    FullName             = fullName,
                    FirstName            = model.FirstName,
                    MiddleName           = model.MiddleName,
                    PhoneNumber          = model.PhoneNumber,
                    LastName             = model.LastName,
                    City                 = model.City,
                    CountryCode          = model.CountryCode,
                    ZipCode              = model.ZipCode,
                    Email                = model.Email,
                    UserName             = model.UserName,
                    PasswordHash         = model.PasswordHash,
                    EmailConfirmed       = false,
                    TwoFactorEnabled     = false,
                    PhoneNumberConfirmed = false,
                    LockoutEndDateUtc    = null,
                    LockoutEnabled       = true,
                    AccessFailedCount    = 0
                };
                var CreateNewUser = await UserManager.CreateAsync(NewUser, model.PasswordHash);

                await context.SaveChangesAsync();

                UserManager.AddToRole(NewUser.Id, "User");


                jsonResult.Data = new { Success = true, RequiresVerification = false };
                return(jsonResult);
            }

            else
            {
                if (result.UserName == model.UserName)
                {
                    jsonResult.Data = new { Success = false, Message = "User Name is exist" }
                }
                ;


                else
                {
                    jsonResult.Data = new { Success = false, Message = "Email is exist" }
                };
            }
            return(jsonResult);
        }