示例#1
0
        private void SignInAsync(dynamic user, bool isPersistent)
        {
            var userId     = user.Id.ToString(CultureInfo.InvariantCulture);
            var menus      = _menuService.GetIdsPorProfile(user.ProfileId);
            var hierarchys = user.HierarchyId != null
                ? _hierarchyService.GetHierarchyIdsForUser(user.HierarchyId)
                : user.ProfileId.Equals(ProfileResource.Administrator)
                    ? _hierarchyService.GetAllHierarchyIds()
                    : null;

            var clains = new List <Claim>
            {
                new Claim(ClaimTypes.Authentication, userId),
                new Claim(ClaimTypes.NameIdentifier, userId),
                new Claim(ClaimTypes.Role, user.Profile.Nome),
                new Claim(ClaimTypes.Name, user.Nome),
                new Claim(ClaimTypes.Email, user.Email),
                new Claim("Login", user.Login),
                new Claim("Token", _userService.GenerateToken((long)user.Id)),
                new Claim("Cpf", user.Cpf.ToString(CultureInfo.InvariantCulture)),
                new Claim("ProfileId", user.ProfileId.ToString(CultureInfo.InvariantCulture)),
                new Claim("HierarchyId", user.HierarchyId != null
                    ? user.HierarchyId.ToString(CultureInfo.InvariantCulture)
                    : string.Empty),
                new Claim("MenuIds", string.Join("|", menus)),
                new Claim("HierarchyIds", string.Join("|", hierarchys ?? "0"))
            };

            var identity    = new ClaimsIdentity(clains, DefaultAuthenticationTypes.ApplicationCookie);
            var ctx         = Request.GetOwinContext();
            var authManager = ctx.Authentication;

            authManager.SignIn(new AuthenticationProperties {
                IsPersistent = isPersistent
            }, identity);
        }