Beispiel #1
0
        // 添加账户
        public JsonResult CreateAccount()
        {
            var    stream           = new StreamReader(Request.InputStream);
            string str              = stream.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();

            try
            {
                var datas = js.Deserialize <Dictionary <string, object> >(str);
                accountService.CreateAccount(datas[key.username].ToString(), datas[key.jobnumber].ToString())
                .SetPassWord("1")
                .SetCanDelete(true);
                Account       account = accountService.GetOneAccount(AccountIdentifier.of(datas[key.username].ToString()));
                List <string> roles   = new List <string>((string[])((ArrayList)datas["roles"]).ToArray(typeof(string)));
                for (var i = 0; i < roles.Count; i++)
                {
                    int role = (int)Enum.Parse(typeof(Role), roles[i]);
                    account.AddRole(role);
                }
                accountService.Commit(); // 一个方法只能Commit一次

                var result = new{ Result = "成功" };
                return(Json(result, JsonRequestBehavior.DenyGet));
            }
            catch (Exception err)
            {
                var result = new{ Result = err.Message };
                return(Json(result, JsonRequestBehavior.DenyGet));
            }
        }
Beispiel #2
0
        // 重置密码
        public void ResetPassword(string username)
        {
            Account account = GetOneAccount(AccountIdentifier.of(username));

            account.PassWord = passwordService.CreateDbPassword("1");
            Commit();
        }
Beispiel #3
0
 public static void CreateAccount()
 {
     if (!repository.IsExisted(new Account.By(AccountIdentifier.of("admin"))))
     {
         accountService.CreateAccount("admin")
         .SetPassWord("1")
         .SetRole(Role.Admin)
         .SetCanDelete(false)
         .Commit();
     }
 }
Beispiel #4
0
        //增
        public IAccountCommand CreateAccount(string username)
        {
            if (repository.IsExisted(new Account.By(AccountIdentifier.of(username))))
            {
                throw new Exception("用户名已存在!");
            }
            var account = new Account(AccountIdentifier.of(username));

            repository.Save(account);
            return(new AccountCommand(account, repository, passwordService));
        }
Beispiel #5
0
        public IAccountCommand CreateAccount(string userName)
        {
            if (IsDuplicateUserName(userName))
            {
                throw new DomainErrorException("用户名已存在!");
            }

            var user = new Account(AccountIdentifier.of(userName));

            repository.Save(user);
            return(new AccountCommand(user, passwordSecurity, repository));
        }
Beispiel #6
0
        //证
        public bool Verify(string username, string password)
        {
            Account account;

            if (username.ToLower().StartsWith("sdt")) // 如果用工号登录
            {
                account = GetOneAccountByjobnumber(username);
            }
            else
            {
                account = GetOneAccount(AccountIdentifier.of(username));
            }
            return(passwordService.ComparePassword(password, account.PassWord));
        }
Beispiel #7
0
        //增
        public IAccountCommand CreateAccount(string username, string jobnumber)
        {
            if (repository.IsExisted(new Account.By(AccountIdentifier.of(username))))
            {
                throw new Exception("用户名已存在!");
            }
            if (repository.IsExisted(new Account.ByJobNumber(jobnumber)))
            {
                throw new Exception("工号已存在!");
            }
            var account = new Account(AccountIdentifier.of(username));

            account.Job_Numner = jobnumber;
            repository.Save(account);
            return(new AccountCommand(account, repository, passwordService));
        }
Beispiel #8
0
        public bool ValidateAccount(string userName, string password, out string errorMessage)
        {
            var id = AccountIdentifier.of(userName);

            if (!repository.IsExisted(new Account.By(id)))
            {
                errorMessage = "用户名不存在";
                return(false);
            }
            var user = GetAccount(id);

            if (!passwordSecurity.ComparePasswords(user.Password, password))
            {
                errorMessage = "用户名或密码不正确";
                return(false);
            }
            errorMessage = "";
            return(true);
        }
Beispiel #9
0
        public static void InitDataBase(bool exec)
        {
            GetSession(exec); //初始化表
            IRepository     repository     = UnityIoC.Get <IRepository>();
            IAccountService accountService = UnityIoC.Get <IAccountService>();

            if (!repository.IsExisted(new Account.By(AccountIdentifier.of("肖斌武")))) //初始化数据
            {
                accountService.CreateAccount("肖斌武", "SDT34200")
                .SetPassWord("1")
                .SetRole(Role.All)
                .SetCanDelete(false);
                accountService.CreateAccount("王旺玲", "SDT02207")
                .SetPassWord("1")
                .SetRole(Role.All)
                .SetCanDelete(false)
                .Commit();
            }
        }
Beispiel #10
0
 public ActionResult Login(FormCollection FC)
 {
     if (string.IsNullOrEmpty(FC[Keys.UserName]) ||
         string.IsNullOrEmpty(FC[Keys.PassWord]))
     {
         ViewData[Keys.ErrorMessage] = "用户名或密码不能为空!";
         return(View());
     }
     if (string.IsNullOrEmpty(FC[Keys.VefCode]))
     {
         ViewData[Keys.ErrorMessage] = "验证码不能为空!";
         return(View());
     }
     if (sessionService.GetVefCode().ToLower() != FC[Keys.VefCode].ToLower())
     {
         ViewData[Keys.ErrorMessage] = "验证码错误!";
         return(View());
     }
     try
     {
         if (!service.Verify(FC[Keys.UserName], FC[Keys.PassWord])) //如果密码不正确,或用户名不存在
         {
             throw new Exception("");
         }
         var account = service.GetOneAccount(AccountIdentifier.of(FC[Keys.UserName]));
         sessionService.Login(FC[Keys.UserName], false);
         sessionService.SaveAccount(account);
         if ((account.Roles & (int)Role.Admin) == (int)Role.Admin)
         {
             return(RedirectToAction("Index", "Admin", new { Area = "Admin" }));
         }
         //return RedirectToAction("Index", "User", new {Area = "User"});
         return(RedirectToAction("Index", "Home"));
     }
     catch (Exception e)
     {
         ViewData[Keys.ErrorMessage] = "用户名或密码错误!";
         return(View());
     }
 }
Beispiel #11
0
        // 登录
        public JsonResult Ajax_Login()
        {
            var stream = new StreamReader(Request.InputStream);
            var str    = stream.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            var datas = js.Deserialize <Dictionary <string, string> >(str);

            try
            {
                if (!service.Verify(datas["username"], datas["password"])) //如果密码不正确,或用户名不存在
                {
                    throw new Exception("");
                }
                Account account;
                if (datas["username"].ToLower().StartsWith("sdt")) // 如果用工号登录
                {
                    account = service.GetOneAccountByjobnumber(datas["username"]);
                }
                else
                {
                    account = service.GetOneAccount(AccountIdentifier.of(datas["username"]));
                }
                sessionService.Login(account.Id.UserName, false);
                sessionService.SaveAccount(account);

                var result = new
                {
                    Result = "成功"
                };
                return(Json(result, JsonRequestBehavior.DenyGet));
            }
            catch
            {
                var result = new
                {
                    Result = "用户名或密码错误!"
                };
                return(Json(result, JsonRequestBehavior.DenyGet));
            }
        }
        // 登录
        public JsonResult Ajax_Login()
        {
            var datas = tool.Deserialize <Dictionary <string, string> >(Request.InputStream);

            try
            {
                if (!service.Verify(datas["username"], datas["password"])) //如果密码不正确,或用户名不存在
                {
                    throw new Exception("有户名或密码错误");
                }
                Account account;
                if (datas["username"].ToLower().StartsWith("sdt")) // 如果用工号登录
                {
                    account = service.GetOneAccountByjobnumber(datas["username"]);
                }
                else
                {
                    account = service.GetOneAccount(AccountIdentifier.of(datas["username"]));
                }
                sessionService.Login(account.Id.UserName, false);
                sessionService.SaveAccount(account);

                var result = new
                {
                    Result = "成功"
                };
                return(Json(result, JsonRequestBehavior.DenyGet));
            }
            catch (Exception err)
            {
                var result = new
                {
                    Result = err.Message
                };
                return(Json(result, JsonRequestBehavior.DenyGet));
            }
        }
Beispiel #13
0
 protected static AccountIdentifier 用户(int no)
 {
     return(AccountIdentifier.of(no.ToString()));
 }
Beispiel #14
0
 public bool IsDuplicateUserName(string userName)
 {
     return(repository.IsExisted(new Account.By(AccountIdentifier.of(userName))));
 }
Beispiel #15
0
        //证
        public bool Verify(string username, string password)
        {
            var account = GetOneAccount(AccountIdentifier.of(username));

            return(passwordService.ComparePassword(password, account.PassWord));
        }
Beispiel #16
0
 //删除账户
 public ActionResult Delete_Account(string id)
 {
     accountService.Delete(AccountIdentifier.of(id));
     return(RedirectToAction("ManageAccount"));
 }
        public bool Permit(string userName, string area, string controller, string action)
        {
            var account = GetAccount(AccountIdentifier.of(userName));

            return(account.GetAuthorities().Permit(area, controller, action));
        }