コード例 #1
0
        public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var info = await Authentication.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return InternalServerError();
            }

            var user = new Account() { UserName = model.Email, Email = model.Email };

            IdentityResult result = await UserManager.CreateAsync(user);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);
            if (!result.Succeeded)
            {
                return GetErrorResult(result); 
            }
            return Ok();
        }
コード例 #2
0
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new Account()
            {
                UserName = model.UserName,
                Email = model.Email
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);
            
            if (result.Succeeded)
            {
                var role = string.Empty;

                if (model.FirstName == null)
                {
                    decimal latitude = 0;
                    decimal longitude = 0;

                    if (model.Latitude != null)
                    {
                        latitude = decimal.Parse(model.Latitude);
                    }

                    if (model.Longitude != null)
                    {
                        longitude = decimal.Parse(model.Longitude);
                    }

                    var farm = new Farm()
                    {
                        Owner = model.Owner,
                        Account = user.UserName,
                        Name = model.Name,
                        Address = model.Address,
                        Phones = model.Phone,
                        Latitude = latitude,
                        Longitude = longitude
                    };

                    this.data.Farms.Add(farm);
                    role = "Farmer";
                }
                else
                {
                    var client = new Client()
                    {
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Account = user.UserName,
                        Phone = model.Phone
                    };

                    this.data.Clients.Add(client);
                    role = "Client";
                }

                this.data.SaveChanges();
                var idResult = UserManager.AddToRole(user.Id, role);

                return Ok(result);
            }
            else
            {
                return GetErrorResult(result);
            } 
        }