public ActionResult Index(LogOnView model, string ReturnUrl)
        {
            //using (SettingDal setting = new SettingDal())
            //{
            //    var path = setting.GetSetting("BK_img", SettingType.Default).Value;
            //    if (!string.IsNullOrEmpty(path))
            //    {
            //        var s = MobileHis.Misc.Storage.GetStorage(StorageScope.backgroundImg);
            //        if (s.FileExist(path))
            //        {
            //            ViewBag.BK_img = path;//s.Open(category, path);
            //        }
            //    }
            //    var hospitalName = setting.GetSetting("Hospital_Name", SettingType.info).Value;
            //    ViewBag.hospitalName = hospitalName;
            //    var partnerPathList = setting.GetPartnerImagePath();
            //    ViewBag.partnerPathList = partnerPathList;
            //}
            //if (!ModelState.IsValid)
            //{
            //    //ViewBag.Message = string.Join(",", ModelState.Values
            //    //                        .SelectMany(x => x.Errors)
            //    //                        .Select(x => x.ErrorMessage));

            //}
            if (ModelState.IsValid)
            {
                var account = _accountService.LogOn(model.Email + Config.AppSetting("EmailDomain"), model.Password);
                if (account == null)
                {
                    ViewBag.Message = "Login Failed";
                }
                else
                {
                    //CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();

                    //serializeModel.ID = account.ID;
                    //serializeModel.Name = account.Name;
                    //serializeModel.Email = account.Email;
                    //serializeModel.Roles = string.Join(",", account.Account2Role.Select(x => x.Role.name).ToArray());
                    WrappedPrincipal serializePrincipal = new WrappedPrincipal(
                        id: account.ID,
                        email: account.Email,
                        name: account.Name,
                        roles: string.Join(",", account.Account2Role.Select(x => x.Role.name).ToArray())
                        );
                    string userData = JsonConvert.SerializeObject(serializePrincipal);

                    FormsAuthenticationTicket authTicket = null;
                    var expiration = model.IsRemember == "1" ? DateTime.Now.AddDays(15) : DateTime.Now.AddHours(3);
                    authTicket = new FormsAuthenticationTicket(1, account.Email, DateTime.Now, expiration, false, userData);
                    string     encTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
                    {
                        Expires = authTicket.Expiration, Path = "/"
                    };

                    Session["userAuth"] = JsonConvert.SerializeObject(
                        _accountService.AuthRole(
                            account.Account2Role.Select(x => x.Role.name).ToList(),
                            Server.MapPath("~/menu_all.xml")));      //為了生成SessionId
                    Response.Cookies.Add(faCookie);
                    #region 紀錄登入資訊

                    Log(account.Name + "  Login", FunctionType.Login, account.Name);
                    #endregion

                    if (string.IsNullOrWhiteSpace(ReturnUrl))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        return(Redirect(ReturnUrl));
                    }
                }
            }

            return(View(model));
        }
Exemple #2
0
 /// <summary>
 /// 設定此Unit of work(UOF)的Context。
 /// </summary>
 /// <param name="context">設定UOF的context</param>
 public EFUnitOfWork(DbContext context, WrappedPrincipal principal)
 {
     _context   = context;
     _principal = principal;
 }
 public AccountService(IUnitOfWork unitOfWork, WrappedPrincipal principal) : base(unitOfWork)
 {
     _principal = principal;
 }