Ejemplo n.º 1
0
        public async Task<IHttpActionResult> Post(RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ApplicationUser user = new ApplicationUser();
                    Mapper.Map(viewModel, user);

                    var identityResult = userManager.Create(user, viewModel.Password);
                    //var userRoleResult = userManager.AddToRole(user.Id, "Member");

                    if (identityResult.Succeeded)
                    {
                        //var userRoleResult = await userManager.AddToRoleAsync(user.Id, "Member");

                        //if (userRoleResult.Succeeded)
                        //{
                        //    //await SignInAsync(user, isPersistent: false);
                        //    //return RedirectToAction("Index", "Home");
                        //    //return Json(new { status = "success" });
                        //    return Ok();
                        //}

                        //return InternalServerError();
                        return Ok();
                    }
                    else
                    {
                        foreach (var error in identityResult.Errors)
                        {
                            ModelState.AddModelError(error,error);
                        }

                        return BadRequest(ModelState);
                    }
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            else
            {
                return BadRequest(ModelState);
            }

        }
Ejemplo n.º 2
0
        private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            try
            {
                AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                var identity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
Ejemplo n.º 3
0
        public async Task<IHttpActionResult> Post(RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ApplicationUser user = new ApplicationUser();
                    Mapper.Map(viewModel, user);

                    var identityResult = await userManager.CreateAsync(user, viewModel.Password);

                    if (identityResult.Succeeded)
                    {
                        userManager.AddToRole(user.Id, "Member");
                        return Ok();
                    }
                    else
                    {
                        foreach (var error in identityResult.Errors)
                        {
                            ModelState.AddModelError(error,error);
                        }

                        return BadRequest(ModelState);
                    }
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            else
            {
                return BadRequest(ModelState);
            }

        }
        public async Task<IHttpActionResult> Post(RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                switch (viewModel.Action)
                {
                    case Enums.LoginActions.Login:
                        {
                            var user = userManager.FindByName(viewModel.Email);

                            if (user == null)
                            {
                                return new ResourceMetadata.Web.Helpers.InvalidUserResult(Request);
                            }

                            await SignInAsync(user, isPersistent: false);
                            return Ok();
                        }
                    case Enums.LoginActions.Register:
                        {
                            try
                            {
                                ApplicationUser user = new ApplicationUser();
                                Mapper.Map(viewModel, user);
                                var identityResult = await userManager.CreateAsync(user);

                                if (identityResult.Succeeded)
                                {
                                    await SignInAsync(user, isPersistent: false);
                                }
                                else
                                {
                                    foreach (var error in identityResult.Errors)
                                    {

                                    }
                                }
                            }
                            catch (Exception ex)
                            {

                                throw ex;
                            }


                            //userService.RegisterUser(user);
                            //var ticket = new FormsAuthenticationTicket(viewModel.Email, true, 3);
                            //var jsonString = JsonConvert.SerializeObject(ticket);
                            //HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)));
                            return Ok();
                        }
                    default:
                        {
                            break;
                        }
                }
            }

            return InternalServerError();
        }
Ejemplo n.º 5
0
 public ApplicationUser RegisterUser(ApplicationUser user)
 {
     user =userRepository.Add(user);
     SaveChanges();
     return user;
 }
Ejemplo n.º 6
0
 public void UpdateUser(ApplicationUser user)
 {
     userRepository.Update(user);
     SaveChanges();
 }