Exemplo n.º 1
0
        public async Task <JsonResult> Create(UserApp user, string password)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserApp u = new UserApp();
                    u.UserName          = user.Email;
                    u.LastLoginDateTime = DateTime.Now;
                    u.FullName          = user.FullName;
                    u.PhoneNumber       = user.PhoneNumber;
                    u.Email             = user.Email;

                    IdentityResult UserAddResult = await UserManagerApp.CreateAsync(u, password);

                    if (UserAddResult.Succeeded)
                    {
                        UserApp thisUser = UserManagerApp.Find(user.Email, password);
                        if (thisUser != null)
                        {
                            ClaimsIdentity Ident = UserManagerApp.CreateIdentity(thisUser, DefaultAuthenticationTypes.ApplicationCookie);
                            HttpContext.GetOwinContext().Authentication.SignOut();

                            HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties()
                            {
                                IsPersistent = false
                            }, Ident);
                            return(Json(new { status = "login" }));
                        }

                        return(Json(new { status = "ok" }));
                        //IdentityResult RoleAddResult = await UserManagerApp.AddToRoleAsync(u.Id, "User");
                        //if (RoleAddResult.Succeeded)
                        //{
                        //   // Success("User Added Successfuly!");
                        //    return Json(new { status = "ok" });
                        //}

                        //else
                        //{
                        //    return Json(new { status = "error",message= UserAddResult.Errors});
                        //}
                    }

                    else
                    {
                        return(Json(new { status = "error", message = UserAddResult.Errors }));
                    }
                }
                else
                {
                    return(Json(new { status = "error", message = new { text = "error" } }));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public JsonResult Login(UserApp user, string password)
        {
            UserApp thisUser = UserManagerApp.Find(user.Email, password);

            if (thisUser != null)
            {
                ClaimsIdentity Ident = UserManagerApp.CreateIdentity(thisUser, DefaultAuthenticationTypes.ApplicationCookie);
                HttpContext.GetOwinContext().Authentication.SignOut();

                HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties()
                {
                    IsPersistent = false
                }, Ident);
                return(Json(new { status = "ok" }));
            }
            else
            {
                return(Json(new { status = "error" }));
            }
        }