예제 #1
0
        public virtual async Task <ActionResult> RegisterUser(RegisterUserViewModel registerUserViewModel)
        {
            try
            {
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    if (!ModelState.IsValid)
                    {
                        //var errorList = ModelState.Values.SelectMany(m => m.Errors)
                        //                 .Select(e => e.ErrorMessage)
                        //                 .ToList();
                        ModelState.AddModelError("", @"ورودی نامعتبر!");
                        return(View(registerUserViewModel));
                    }

                    var addUser =
                        await _authenticationManagementService.AddUser(registerUserViewModel.IdentificationCode,
                                                                       registerUserViewModel.Email, registerUserViewModel.Password, registerUserViewModel.Name,
                                                                       registerUserViewModel.Family, registerUserViewModel.NationalCode,
                                                                       registerUserViewModel.Mobile);

                    if (addUser.Item1)
                    {
                        var callbackUrl = Url.Action(MVC.Account.ActionNames.ConfirmEmail, MVC.Account.Name,
                                                     new { userId = addUser.Item4.Id, code = addUser.Item3 }, Request.Url?.Scheme);
                        //await _userManager.SendEmailAsync(user.Id, "تائید حساب کاربری ","جهت فعال سازی حساب کاربری خود بر روی لینک زیر کلیک کنید: <a href=\"" + callbackUrl +"\">link</a>");
                        ViewBag.Link = callbackUrl;
                        var     univers = _universityRepository.All().FirstOrDefault();
                        dynamic email   = new Email("Reg.Html");
                        email.To      = addUser.Item4.Email;
                        email.Title   = "تائید حساب کاربری ";
                        email.Subject = (univers == null ? "تائید حساب کاربری " : "تائید حساب کاربری " + univers.Name);
                        email.Body    = "جهت فعال سازی حساب کاربری خود بر روی لینک زیر کلیک کنید ";
                        email.Link    = callbackUrl;
                        if (!string.IsNullOrWhiteSpace(email.To.ToString()))
                        {
                            //  email.Send();
                            //_unitOfWork.CommitTransaction();
                            scope.Complete();
                        }

                        return(View(MVC.Account.Views.DisplayEmail));
                    }
                    ModelState.AddModelError("InvalidUser", addUser.Item2);
                    return(View());
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("InvalidUser", @"شما قادر به ثبت نام نمی باشید!");
                return(View());
            }
        }
 public string GetLogoUrl(AddressUrlFile addressUrl)
 {
     try
     {
         var logoname = _universityRepository.All().Select(sel => sel.Logo).FirstOrDefault();
         return(addressUrl.Logo + logoname);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
예제 #3
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);
            }
        }