Пример #1
0
        public async Task <bool> OnLogin()
        {
            string   userNameForauthen = Session["userLogin"].ToString();
            string   unitNameForauthen = Session["unitLogin"].ToString();
            UnitsDAL unitCtrl          = new UnitsDAL();
            Units    unitObj           = unitCtrl.GetUnitByCode(unitNameForauthen);

            try
            {
                var             userManager = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
                var             roleManager = System.Web.HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
                ApplicationUser user        = userManager.FindByName(userNameForauthen);


                if (user != null)
                {
                    SysBaseInfor.SetCurrentUserName(user.UserName);
                    SysBaseInfor.SetCurrentUserDisplayName(user.DisplayName);
                    SysBaseInfor.SetCurrentUserId(user.Id);
                    SysBaseInfor.SetCurrentUserType(user.Type);
                    SysBaseInfor.SetCurrentUserTypeInfo(user.TypeInfo);
                    SysBaseInfor.SetIdNguoiDung(user.TypeInfo.ToString());

                    SysBaseInfor.SetCurrentUnitId(unitObj.Id);
                    SysBaseInfor.SetCurrentUnitCode(unitObj.Code);
                    SysBaseInfor.SetCurrentUnitName(unitObj.Name);
                    SysBaseInfor.SetCurrentMediaURL(unitObj.MediaUrl);
                    SysBaseInfor.SetCurrentUnitLogoUrl(unitObj.LogoUrl);

                    var lstRoleNames = userManager.GetRoles(user.Id);
                    if (lstRoleNames.Count > 0)
                    {
                        SYS_MENU_ROLE_CTRL smrCtrl = new SYS_MENU_ROLE_CTRL();
                        var tmpLstRoles            = new List <SYS_MENU_ROLE>();
                        foreach (string tmpRoleName in lstRoleNames)
                        {
                            if (!string.IsNullOrEmpty(tmpRoleName))
                            {
                                var tmpRole  = roleManager.FindByName(tmpRoleName);
                                var lstRoles = smrCtrl.GetLstSysMenuRoleWithView(tmpRole.Id);
                                foreach (SYS_MENU_ROLE tmpSmr in lstRoles)
                                {
                                    if (tmpSmr.ID > 0)
                                    {
                                        if (tmpLstRoles.All(r => r.ID_SYS_MENU != tmpSmr.ID_SYS_MENU))
                                        {
                                            tmpLstRoles.Add(tmpSmr);
                                        }
                                        else
                                        {
                                            var index         = tmpLstRoles.FindIndex(r => r.ID_SYS_MENU == tmpSmr.ID_SYS_MENU);
                                            var tmpAccessOld  = tmpLstRoles[index].ACCESS_RIGHT;
                                            var tmpAccessNew  = tmpSmr.ACCESS_RIGHT;
                                            var tmpAccessJoin = "";
                                            if (tmpAccessOld != tmpAccessNew)
                                            {
                                                for (int i = 0; i < tmpAccessOld.Length; i++)
                                                {
                                                    tmpAccessJoin += (tmpAccessOld[i] > tmpAccessNew[i]) ? tmpAccessOld[i] : tmpAccessNew[i];
                                                }
                                                tmpLstRoles[index].ACCESS_RIGHT = tmpAccessJoin;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        var jsonLstRoles = JsonConvert.SerializeObject(tmpLstRoles);
                        SysBaseInfor.SetCurrentUserListMenu(jsonLstRoles);
                    }

                    var sign = await SignInHelper.SignInOrTwoFactor(user, true);
                }
            }
            catch (Exception ex)
            {
                //AuthenticationManager.SignOut();
                //Session.Abandon();
                //ModelState.AddModelError("", ex);
                //return View(model);
                return(false);
            }
            return(true);
        }