public virtual JsonResult GetLoggedUser()
        {
            var response = new { Success = false, Data = "" };

            var aspnetEmail = GetMyAspnetEmail();

            if (string.IsNullOrEmpty(aspnetEmail))
            {
                return(Json(response, JsonRequestBehavior.AllowGet));
            }
            var user = UserCore.GetEFByAspNetEmail(aspnetEmail);

            if (user == null)
            {
                return(Json(response, JsonRequestBehavior.AllowGet));
            }

            var userModel = new UserLoginResponse
            {
                Id                = user.Id,
                OrganizationId    = user.OrganizationId,
                FirstName         = user.FirstName,
                LastName          = user.LastName,
                FullName          = user.FirstName,
                Email             = user.AspNetUser.Email,
                DisplayName       = user.DisplayName,
                ProfilePictureUrl = user.ProfilePictureUrl,
                RolesId           = AspNetUserCore.SetRolesId(user.AspNetUser.AspNetRoles),
            };

            return(Json(new { Success = true, Data = userModel }, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public virtual async Task <JsonResult> Login(LoginViewModel model)
        {
            var response = new { Sucess = false, data = "" };
            var password = HashPassword(model.Password);

            if (!ModelState.IsValid)
            {
                return(Json(new { Sucess = false, Data = "" }));
            }

            var applicationUser = await CheckEmailAndPassword(model.Email, model.Password).ConfigureAwait(false);

            if (applicationUser == null)
            {
                return(Json(new { Sucess = false, Data = "" }));
            }
            var userResponse = AspNetUserCore.HasValidRole(model.Email);

            if (userResponse == null)
            {
                return(Json(new { Sucess = false, Data = "" }));
            }

            CreateCookie(applicationUser.UserName, applicationUser.Id);
            return(Json(new { Sucess = true, Data = userResponse }));
        }
        public JsonResult Activate(Guid userId)
        {
            if (userId == Guid.Empty)
            {
                return(Json(ResponseFactory.ErrorReponse));
            }

            var user = UserCore.Get(userId, new[] { nameof(datalayer.User.AspNetUser) });

            if (user == null)
            {
                return(Json(ResponseFactory.ErrorReponse));
            }

            user.AspNetUser.Status = 0;

            var updatedUser = AspNetUserCore.Update(user.AspNetUser);

            if (updatedUser == null)
            {
                return(Json(ResponseFactory.ErrorReponse));
            }

            return(Json(ResponseFactory.SuccessResponse));
        }
        //public virtual async Task<ActionResult> Register(RegisterViewModel model)
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        RedirectToAction("Login/Index");
        //    }

        //    var createdUserResponse = await UserCore.RegisterUser(model).ConfigureAwait(false);
        //    if (!ResponseFactory.IsSuccessful(createdUserResponse))
        //    {
        //        RedirectToAction("Login/Index");
        //    }

        //    RedirectToAction("Login/Index");
        //}

        public static Response CheckEmailCollision(string email)
        {
            var response = ResponseFactory.Error <AspNetUser>();

            if (string.IsNullOrWhiteSpace(email))
            {
                return(response);
            }

            var existingAspNetUser = AspNetUserCore.GetByEmail(email);

            if (existingAspNetUser != null)
            {
                return(ResponseFactory.ErrorReponse);
            }

            return(ResponseFactory.Success());
        }
        protected async Task <bool> CheckUserCredentials(string email, string password)
        {
            try
            {
                var applicationUser = await CheckEmailAndPassword(email, password).ConfigureAwait(false);

                if (applicationUser == null)
                {
                    return(false);
                }

                IList <string> roleIds = null;

                var roleIdList = await AspNetRoleCore.GetListAsync(t => t.Id != Guid.Empty.ToString()).ConfigureAwait(false);

                if (roleIdList == null)
                {
                    roleIds = roleIdList.Select(t => t.Id).ToList();
                }



                var isUserApproved = await AspNetUserCore.IsUserIdAuthorizedAsync(applicationUser.Id, roleIds.ToList()).ConfigureAwait(false);

                if (!isUserApproved)
                {
                    return(false);
                }

                CreateCookie(applicationUser.UserName, applicationUser.Id);

                return(true);
            }
            catch (Exception ex)
            {
                //LogHelper.LogException<AccountController>(ex);
            }
            return(false);
        }
        public async Task <ActionResult> RegisterAdmin(RegisterViewModel registerModel, bool refreshFromDb = false)
        {
            var createUserResponse = CheckEmailCollision(registerModel.Email);

            if (!ResponseFactory.IsSuccessful(createUserResponse))
            {
                return(View("~/Views/Account/Login.cshtml"));
            }

            var passwordHash = HashPassword(registerModel.Password);

            registerModel.userType    = (int)UserType.ADMIN;
            registerModel.PhoneNumber = "0123456789";
            registerModel.Id          = Guid.NewGuid();

            var registerUserResponse = await AspNetUserCore.Register(registerModel, passwordHash).ConfigureAwait(false);

            if (!ResponseFactory.IsSuccessful(registerUserResponse))
            {
                return(View("~/Views/Account/Login.cshtml"));
            }

            return(View("~/Views/Account/Login.cshtml"));
        }
Example #7
0
 protected async Task <AspNetUser> GetLoggedUser(IList <string> navigationProperties = null)
 {
     return(await AspNetUserCore.GetAsync(GetMyAspnetUserId(), navigationProperties).ConfigureAwait(false));
 }