Exemplo n.º 1
0
        public async Task <ClaimsIdentity> GenerateUserIdentityAsync(IApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            // Add custom user claims here
            return(userIdentity);
        }
        public virtual async Task SignInAsync(AppUser user, bool isPersistent, bool rememberBrowser)
        {
            // Clear any partial cookies from external or two factor partial sign ins
            SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
            var userIdentity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie).ConfigureAwait(false);

            if (rememberBrowser)
            {
                var rememberBrowserIdentity = CreateTwoFactorRememberBrowserIdentity(user.Id);
                _authenticationManager.SignIn(new AuthenticationProperties {
                    IsPersistent = isPersistent
                }, userIdentity, rememberBrowserIdentity);
            }
            else
            {
                _authenticationManager.SignIn(new AuthenticationProperties {
                    IsPersistent = isPersistent
                }, userIdentity);
            }
        }
Exemplo n.º 3
0
        private async Task <bool> SignInAsync(AppUser user, bool isPersistent)
        {
            var myuser = await _userManager.FindCustomByIdAsync(user.Id);

            _authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            if (identity == null)
            {
                return(false);
            }
            if (myuser.Profile != null)
            {
                identity.AddClaim(new Claim("FullName", myuser.Profile.Name + " " + myuser.Profile.Family));
                identity.AddClaim(new Claim("FirstName", myuser.Profile.Name));
                identity.AddClaim(new Claim("LastName", myuser.Profile.Family));
                if (myuser.Profile.Avatar != null)
                {
                    identity.AddClaim(new Claim("Avatar", myuser.Profile.Avatar));
                }
            }
            var def = _definitionService.GetAllDefinitionsAsQueryable().FirstOrDefault();

            if (def != null)
            {
                if (def.UniversityName != null)
                {
                    identity.AddClaim(new Claim("UniversName", def.UniversityName));
                }
                if (def.Logo != null)
                {
                    identity.AddClaim(new Claim("Logo", def.Logo));
                }
            }
            _authenticationManager.SignIn(new AuthenticationProperties {
                IsPersistent = isPersistent
            }, identity);
            return(true);
        }
Exemplo n.º 4
0
        public async Task ImpersonateUserAsync()
        {
            var context          = System.Web.HttpContext.Current;
            var originalUsername = context.User.Identity.Name;
            var myuser           = _userService.Find(int.Parse(User.Identity.GetUserId()));

            var impersonatedUser = await _userManager.FindByIdAsync(int.Parse(User.Identity.GetUserId()));

            var impersonatedIdentity = await _userManager.CreateIdentityAsync(impersonatedUser, DefaultAuthenticationTypes.ApplicationCookie);

            impersonatedIdentity.AddClaim(new Claim("FullName", myuser.Profile.Name + " " + myuser.Profile.Family));
            impersonatedIdentity.AddClaim(new Claim("FirstName", myuser.Profile.Name));
            impersonatedIdentity.AddClaim(new Claim("LastName", myuser.Profile.Family));
            if (myuser.Profile.Avatar != null)
            {
                impersonatedIdentity.AddClaim(new Claim("Avatar", myuser.Profile.Avatar));
            }
            var def = _definitionService.GetAllDefinitionsAsQueryable().FirstOrDefault();

            if (def != null)
            {
                if (def.UniversityName != null)
                {
                    impersonatedIdentity.AddClaim(new Claim("UniversName", def.UniversityName));
                }
                if (def.Logo != null)
                {
                    impersonatedIdentity.AddClaim(new Claim("Logo", def.Logo));
                }
            }
            var authenticationManager = context.GetOwinContext().Authentication;

            authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
            authenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = false
            }, impersonatedIdentity);
        }
 private async Task SignInAsync(AppUser user, bool isPersistent)
 {
     _userManager.SignOut(_userManager.ExternalCookie, _userManager.TwoFactorCookie);
     _userManager.SignIn(isPersistent, await _userManager.CreateIdentityAsync(user, _userManager.ApplicationCookie));
 }
Exemplo n.º 6
0
        /// <summary>
        /// claim کاربر را مقداردهی می کند
        /// </summary>
        /// <param name="user"></param>
        /// <param name="isPersistent"></param>
        /// <returns></returns>
        public async Task <bool> SignInAsync(User user, bool isPersistent)
        {
            try
            {
                string levelId = "", levelProgram = "", organName = "", isAdmin = "false", isPersonel = "false";

                var userProfile = _userRepository.Where(u => u.Id == user.Id).Include(i => i.Authentication)
                                  .Include(i => i.Authentication.CentralOrganization).Include(i => i.Authentication.BranchProvince)
                                  .Include(i => i.Authentication.University).Include(i => i.Person).Include(i => i.Person.Profile)
                                  .Include(i => i.Person.Personel).Include(i => i.Roles.Select(s => s.Role)).FirstOrDefault();

                if (userProfile != null)
                {
                    if (userProfile.Authentication.CentralOrganizationId > 0)
                    {
                        levelId      = userProfile.Authentication.BranchProvinceId.ToString();
                        levelProgram = LevelProgram.CentralOrganization.ToString();
                        organName    = userProfile.Authentication.CentralOrganization.Name;
                    }
                    else if (userProfile.Authentication.BranchProvinceId > 0)
                    {
                        levelId      = userProfile.Authentication.BranchProvinceId.ToString();
                        levelProgram = LevelProgram.BranchProvince.ToString();
                        organName    = userProfile.Authentication.BranchProvince.Name;
                    }
                    else
                    {
                        levelId      = userProfile.Authentication.UniversityId.ToString();
                        levelProgram = LevelProgram.University.ToString();
                        organName    = userProfile.Authentication.University.Name;
                    }
                    var aa = userProfile.Roles.Any(a => a.Role.RoleType == RoleType.AdminUniversity);
                    isAdmin    = userProfile.Roles.Any(a => a.Role.RoleType == RoleType.AdminUniversity).ToString();
                    isPersonel = (userProfile.Person.Personel != null).ToString();
                }
                var signer     = _signerRepository.Where(s => s.Post.PostPersons.Any(a => a.PersonId == user.Id) || s.Post.UserPosts.Any(a => a.UserId == user.Id)).Select(s => s.RowNumber).DistinctBy(d => d).ToArray();
                var listSigner = string.Join("#", signer);
                var logo       = _universityRepository.All().Select(s => s.Logo).FirstOrDefault() ?? " ";

                _authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

                if (identity == null)
                {
                    return(false);
                }
                if (userProfile?.Person.Profile != null)
                {
                    identity.AddClaim(new Claim("FullName", userProfile.Person.Profile.FullName));
                    identity.AddClaim(new Claim("FirstName", userProfile.Person.Profile.Name));
                    identity.AddClaim(new Claim("LastName", userProfile.Person.Profile.Family));
                    identity.AddClaim(new Claim("AuthenType", userProfile.Authentication.AuthenticationType.ToString()));
                    identity.AddClaim(new Claim("LevelId", levelId));
                    identity.AddClaim(new Claim("LevelProgram", levelProgram));
                    identity.AddClaim(new Claim("OrganName", organName));
                    if (userProfile.Person.Profile.Avatar != null)
                    {
                        identity.AddClaim(new Claim("Avatar", userProfile.Person.Profile.Avatar));
                    }
                }
                identity.AddClaim(new Claim("ListSigner", listSigner));
                identity.AddClaim(new Claim("Logo", logo));
                identity.AddClaim(new Claim("IsAdmin", isAdmin));
                identity.AddClaim(new Claim("IsPersonel", isPersonel));
                _authenticationManager.SignIn(new AuthenticationProperties {
                    IsPersistent = isPersistent
                }, identity);
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }