Exemple #1
0
        public async Task <ActionResult> Edit(int Id)
        {
            var user = await _userManager.FindByIdAsync(Id);

            if (user != null)
            {
                AccountViewModel model = new AccountViewModel();
                model.UserName           = user.UserName;
                model.FIO                = user.FIO;
                model.Position           = user.Position;
                model.SetPasswordEnabled = user.SetPasswordEnabled;
                model.LockoutEnabled     = user.LockoutEnabled;

                //await userManager.UpdateAsync(user);
                IEnumerable <string> Roles = await _userManager.GetRolesAsync(Id);

                if (Roles != null)
                {
                    model.UserRole = Roles.FirstOrDefault();
                }
                else
                {
                    model.UserRole = "";
                }
                return(View(model));
            }
            return(RedirectToAction("List"));
        }
Exemple #2
0
        public async Task <IHttpActionResult> GetUserInfo()
        {
            //var userClaim = Authentication.User;
            //var loginId = userClaim.Identity.GetUserId();
            var loginId = User.Identity.GetUserId();
            var user    = await _userManager.FindByIdAsync(loginId);

            if (user == null)
            {
                return(NotFound());
            }
            var listRole = await _userManager.GetRolesAsync(user.Id);

            UserInfoViewModel viewModel = new UserInfoViewModel
            {
                Id          = user.Id,
                Email       = user.Email,
                UserName    = user.UserName,
                PhoneNumber = user.PhoneNumber,
                Roles       = listRole,
                Address     = user.Address
            };

            return(Ok(viewModel));
        }
        /// <summary>
        /// stop following the target
        /// </summary>
        /// <param name="targetUserId">target(followee) user id</param>
        /// <param name="followerUserId">the follower user id</param>
        public void UnFollow(Guid targetUserId, Guid followerUserId)
        {
            var targetPid   = MyUserManager.FindByIdAsync(targetUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];
            var followerPid = MyUserManager.FindByIdAsync(followerUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];

            FollowService.UnFollow(targetPid, followerPid);
        }
        /// <summary>
        /// Am i following this user
        /// </summary>
        /// <param name="targetUserId">the followee user id</param>
        /// <param name="followerUserId">the follower userid</param>
        /// <returns></returns>
        public bool AmIFollowing(Guid targetUserId, Guid followerUserId)
        {
            var targetPid   = MyUserManager.FindByIdAsync(targetUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];
            var followerPid = MyUserManager.FindByIdAsync(followerUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];

            return(FollowService.AmIFollowing(targetPid, followerPid));
        }
Exemple #5
0
        public async Task <IdentityResult> RefreshUserGroupRolesAsync(long userId)
        {
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new ArgumentNullException("User");
            }
            // Remove user from previous roles:
            var oldUserRoles = await _userManager.GetRolesAsync(userId);

            if (oldUserRoles.Count > 0)
            {
                //await _userManager.RemoveFromRolesAsync(userId, oldUserRoles.ToArray());
            }

            // Find the roles this user is entitled to from group membership:
            var newGroupRoles = await this.GetUserGroupRolesAsync(userId);

            // Get the damn role names:
            var allRoles = await _roleManager.Roles.ToListAsync();

            var addTheseRoles = allRoles.Where(r => newGroupRoles.Any(gr => gr.RoleId == r.Id));
            var roleNames     = addTheseRoles.Select(n => n.Name).ToArray();

            // Add the user to the proper roles
            //await _userManager.AddToRolesAsync(userId, roleNames);

            return(IdentityResult.Success);
        }
        /// <summary>
        /// let the followerId follow the target id
        /// </summary>
        /// <param name="targetUserId">target(followee) user id</param>
        /// <param name="followerUserId">the follower user id</param>
        public void Follow(Guid targetUserId, Guid followerUserId)
        {
            //todo limit the following count here
            var targetPid   = MyUserManager.FindByIdAsync(targetUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];
            var followerPid = MyUserManager.FindByIdAsync(followerUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];

            FollowService.Follow(targetPid, followerPid);
        }
        public IHttpActionResult Get(int id)
        {
            AccountViewModel accountViewModel = new AccountViewModel();

            /*_db.t_User.Where(w => w.UserID == id)
             *  .Select(s => new AccountViewModel() {Id = s.UserID, UserName = s.UserName, FIO = s.FIO,Position = s.Position,SetPasswordEnabled = s.SetPasswordEnabled,LockoutEnabled = s.LockoutEnabled})
             *  .FirstOrDefault();*/
            var user = _userManager.FindByIdAsync(id).Result;

            accountViewModel.Id                 = user.UserID;
            accountViewModel.UserName           = user.UserName;
            accountViewModel.FIO                = user.FIO;
            accountViewModel.Position           = user.Position;
            accountViewModel.SetPasswordEnabled = user.SetPasswordEnabled;
            accountViewModel.LockoutEnabled     = user.LockoutEnabled;
            string userRole = _userManager.GetRolesAsync(id).Result.FirstOrDefault();

            if (userRole != null)
            {
                accountViewModel.UserRole = userRole;
            }
            return(Ok(accountViewModel));
        }
        public async Task <ActionResult> Disassociate(string loginProvider, string providerKey)
        {
            ManageMessageId?message = null;
            var             result  = await UserManager.RemoveLoginAsync(long.Parse(User.Identity.GetUserId()), new UserLoginInfo(loginProvider, providerKey));

            if (result.Succeeded)
            {
                var user = await UserManager.FindByIdAsync(long.Parse(User.Identity.GetUserId()));
                await SignInAsync(user, false);

                message = ManageMessageId.RemoveLoginSuccess;
            }
            else
            {
                message = ManageMessageId.Error;
            }
            return(RedirectToAction(
                       "Manage",
                       new
            {
                Message = message
            }));
        }
        /// <summary>
        /// toggle the follow of a user
        /// </summary>
        /// <param name="targetUserId"></param>
        /// <param name="followerUserId"></param>
        /// <returns></returns>
        public bool ToggoleFollow(Guid targetUserId, Guid followerUserId)
        {
            var targetPid   = MyUserManager.FindByIdAsync(targetUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];
            var user        = MyUserManager.FindByIdAsync(targetUserId).Result;
            var followerPid = MyUserManager.FindByIdAsync(followerUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];

            if (FollowService.AmIFollowing(targetPid, followerPid))
            {
                FollowService.UnFollow(targetPid, followerPid);
                return(false);
            }
            else
            {
                FollowService.Follow(targetPid, followerPid);
                return(true);
            }
        }
        /// <summary>
        /// 获得关注该用户的所有用户id
        /// </summary>
        /// <param name="followeeUserId">该用户id</param>
        /// <returns></returns>
        public IEnumerable <Guid> GetAllFollowerUserIds(Guid followeeUserId)
        {
            //找到该用户的profile的id
            Guid followeeProfileId = MyUserManager.FindByIdAsync(followeeUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];

            QueryObject <OldHouseUserProfile> queryObject = new QueryObject <OldHouseUserProfile>(ProfileService.EntityRepository);
            Dictionary <string, object>       queryDict   = new Dictionary <string, object>();

            queryDict.Add("FollowingIds", followeeProfileId);
            queryObject.AppendQuery(queryDict, QueryLogic.And);
            List <Guid> userIds  = new List <Guid>();
            var         profiles = ProfileService.EntityRepository.Find(queryObject);

            foreach (var profile in profiles)
            {
                userIds.Add(profile.UserId);
            }
            return(userIds);
        }
        public async Task <ActionResult> RemoveLogin(string loginProvider, string providerKey)
        {
            ManageMessageId?message;
            var             result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey));

            if (result.Succeeded)
            {
                var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

                if (user != null)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                }
                message = ManageMessageId.RemoveLoginSuccess;
            }
            else
            {
                message = ManageMessageId.Error;
            }
            return(RedirectToAction("ManageLogins", new { Message = message }));
        }
    public async Task GetProfileDataAsync(ProfileDataRequestContext context)
    {
        var sub = context.Subject.FindFirst("sub")?.Value;

        if (sub != null)
        {
            var user = await _myUserManager.FindByIdAsync(sub);

            var cp = await getClaims(user);

            var claims = cp.Claims;
            if (context.AllClaimsRequested == false ||
                (context.RequestedClaimTypes != null && context.RequestedClaimTypes.Any()))
            {
                claims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToArray().AsEnumerable();
            }
            context.IssuedClaims = claims;
        }
    }
        /// <summary>
        /// 获得该用户主动follow(关注)的用户
        /// </summary>
        /// <param name="followerUserId"></param>
        /// <returns></returns>
        public IEnumerable <OldHouseUser> GetAllFollowingUser(Guid followerUserId)
        {
            //IEnumerable<Guid> followingIds = GetAllFollowingIds(followerUserId);
            //List<OldHouseUser> result = new List<OldHouseUser>();
            //foreach(Guid followingId in followingIds)
            //{
            //    result.Add(MyUserManager.FindByIdAsync(followingId).Result);
            //}
            //return result;

            IEnumerable <Guid>  followingUserProfileIds = GetAllFollowingIds(followerUserId);
            List <OldHouseUser> result = new List <OldHouseUser>();

            foreach (Guid profileId in followingUserProfileIds)
            {
                Guid userId = ProfileService.FindOneById(profileId).UserId;
                result.Add(MyUserManager.FindByIdAsync(userId).Result);
            }
            return(result);
        }
        /// <summary>
        /// get all followee ids for a follower
        /// </summary>
        /// <param name="followerUserId"></param>
        /// <returns></returns>
        public IEnumerable <Guid> GetAllFollowingIds(Guid followerUserId)
        {
            var followerPid = MyUserManager.FindByIdAsync(followerUserId).Result.Profiles[OldHouseUserProfile.PROFILENBAME];

            return(FollowService.GetAllFollowingIds(followerPid));
        }
        public static MvcHtmlString GetUserName(this HtmlHelper html, string id)
        {
            MyUserManager userManager = HttpContext.Current.GetOwinContext().GetUserManager <MyUserManager>();

            return(new MvcHtmlString(userManager.FindByIdAsync(id).Result.UserName));
        }