public int AddUser(int passportUserID)
 {
     User user = new User();
     user.PassportUserID = passportUserID;
     cqgj.AddToUser(user);
     cqgj.SaveChanges();
     return user.UserID;
 }
Beispiel #2
0
 /// <summary>
 /// (与用户中心)同步用户信息
 /// </summary>
 /// <returns></returns>
 public ActionResult GetUsers()
 {
     if (Request.HttpMethod == "POST")
     {
         if (Settings.SSO_Enabled == false)
         {
             return View();
         }
         UserCenterService uc = new UserCenterService();
         var userlist = XmlHandler.ParseUser(uc.GetUsersByCode("zzbgj", "zzbgj", "023034534567898002"));
         int addcount = 0;
         int updatecount = 0;
         foreach (var user in userlist)
         {
             User newuser = new User();
             var temp = from u in CQGJ.User
                        where u.Username == user.jgbm
                        select u;
             if (temp.Count() > 0)
             {
                 newuser = temp.First();
                 newuser.Username = user.yhm;
                 newuser.Password = Core.Security.MD5Encrypt(user.yhmm);
                 newuser.Position = user.zw;
                 newuser.Gender = user.xb;
                 newuser.Telephone = user.bgdh;
                 newuser.Cellphone = user.yddh;
                 updatecount++;
             }
             else
             {
                 newuser.Username = user.yhm;
                 newuser.Password = Core.Security.MD5Encrypt(user.yhmm);
                 newuser.Position = user.zw;
                 newuser.Gender = user.xb;
                 newuser.Telephone = user.bgdh;
                 newuser.Cellphone = user.yddh;
                 CQGJ.AddToUser(newuser);
                 addcount++;
             }
             CQGJ.SaveChanges();
         }
     }
     return View();
 }
        /// <summary>
        /// 用户登录验证
        /// 市委组织部:0230345345678980
        /// 区县组织部:023100
        /// 市级部门:023200
        /// </summary>
        public ActionResult Login()
        {
            if (Request.RequestType == "POST")
            {
                int loginType = GetInt("LoginType");
                string rightnames = "";
                User user = new User();
                Admin admin = new Admin();
                Org org = new Org();
                string username = GetString("username");
                string password = Security.MD5Encrypt(GetString("password"));

                //学员登录流程
                //直接在本系统进行登录验证
                if (loginType == 0)
                {
                    var users = from u in CQGJ.User
                                where u.Username == username && u.Password == password
                                select u;
                    if (users.Count() <= 0)
                    {
                        ViewData["ErrorMessage"] = "用户名或密码有误!";
                        return View();
                    }
                    else
                    {
                        user = users.First();
                    }
                    rightnames = "普通用户";
                    //HttpContext.Session["OrgID"] = ToOrgID(orgCode);
                    //HttpContext.Session["OrgCode"] = orgCode;
                    //HttpContext.Session["PassportUserID"] = user.UserID;
                    //HttpContext.Session["Username"] = user.Username;
                    //HttpContext.Session["OrgType"] = cqgj.GetOrgType(orgCode);
                    Session["User"] = user;
                    Session["UserID"] = user.UserID;

                    // Create a new ticket used for authentication
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1,                                      // Ticket version
                        user.Username,                          // Username associated with ticket
                        DateTime.Now,                           // Date/time issued
                        DateTime.Now.AddMinutes(30),            // Date/time to expire
                        true,                                   // "true" for a persistent user cookie
                        rightnames,                             // User-data, in this case the roles
                        FormsAuthentication.FormsCookiePath);   // Path cookie valid for
                    string hash = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
                    if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
                    Response.Cookies.Add(cookie);

                    return RedirectToAction("index", "user");
                }
                //工作人员登录验证流程
                //先到办公系统验证,当无法与办公系统联系时,直接在本系统进行验证
                //admin测试用户名为admin,密码为testtest
                else if (loginType == 1)
                {
                    string roleName = "";

                    if (Settings.SSO_Enabled == true)
                    {
                        UserCenterService uc = new UserCenterService();
                        LoginInfo loginInfo = XmlHandler.ParseLogin(
                            uc.PublicLogin(Settings.SSO_Username, Settings.SSO_Password,
                           username, GetString("password"), Settings.SSO_AppID));
                        if (loginInfo.Result != 0)
                        {
                            ViewData["ErrorMessage"] = "用户名或密码有误";
                            return View();
                        }
                        admin.Username = loginInfo.User.yhm;
                        string orgcode = loginInfo.UnitList[0].jgbm;
                        org = (from o in CQGJ.Org
                               where o.OrgCode == orgcode
                               select o).First();
                        List<Role> roles = new List<Role>();
                        foreach (string i in loginInfo.Role)
                        {
                            int t = Convert.ToInt32(i);
                            Role temp = (from r in CQGJ.Role
                                        where r.RoleID == t
                                        select r).FirstOrDefault();
                            roles.Add(temp);
                        }
                        //要改成支持多个角色
                        if (roles.Count() > 0)
                        {
                            roleName = roles.First().RoleName;
                        }
                        else
                        {
                            ViewData["ErrorMessage"] = "该用户未被授权访问,如有疑问,请联系管理员!";
                            return View();
                        }
                    }
                    else
                    {
                        var admins = from a in CQGJ.Admin
                                     where a.Username == username && a.Password == password
                                     select a;

                        if (admins.Count() <= 0)
                        {
                            ViewData["ErrorMessage"] = "用户名或密码有误";
                            return View();
                        }
                        else
                        { admin = admins.First(); }

                        org = (from o in CQGJ.Org
                               where o.OrgID == admin.OrgID
                               select o).First();

                        var roles = from r in CQGJ.Role
                                    from ur in CQGJ.UsersInRoles
                                    where ur.AdminID == admin.AdminID && ur.RoleID == r.RoleID
                                    select r;
                        //要改成支持多个角色
                        roleName = roles.First().RoleName;
                    }

                    string[] rightList = { };
                    var rights = from r in CQGJ.Role
                                 from rr in CQGJ.RightsofRoles
                                 from ri in CQGJ.Right
                                 where r.RoleName == roleName && rr.RoleID == r.RoleID && rr.RightID == ri.RightID
                                 select ri;
                    foreach (var r in rights)
                    { rightnames += r.RightName + ","; }
                    rightnames += "管理员";
                    if (roleName == "超级管理员")
                    { rightnames = roleName; }
                    Session["RightList"] = rightnames;
                    Session["OrgType"] = org.OrgType;
                    Session["Org"] = org;
                    Session["Admin"] = admin;

                    // Create a new ticket used for authentication
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1,                                      // Ticket version
                        admin.Username,                         // Username associated with ticket
                        DateTime.Now,                           // Date/time issued
                        DateTime.Now.AddMinutes(30),            // Date/time to expire
                        true,                                   // "true" for a persistent user cookie
                        rightnames,                             // User-data, in this case the roles
                        FormsAuthentication.FormsCookiePath);   // Path cookie valid for
                    // Encrypt the cookie using the machine key for secure transport
                    string hash = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
                    if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
                    Response.Cookies.Add(cookie);

                    return RedirectToAction("index", "admin");
                }
            }
            return View();
        }
Beispiel #4
0
        /// <summary>
        /// 添加学员
        /// </summary>
        /// <param name="id">归属单位ID(注意和工作单位的区别)</param>
        /// <returns></returns>
        public ActionResult AddUser(int id)
        {
            AdminViewData viewData = new AdminViewData();
            var org = (from o in CQGJ.Org
                       where o.OrgID == id
                       select o).First();
            viewData.Org = org;

            if (Request.HttpMethod == "POST")
            {
                string idcard = GetString("IDCard");
                string username = GetString("Username");
                if (username != "" && idcard.Length >= 6)
                {
                    var users = from u in CQGJ.User
                                where u.Username == username
                                select u;
                    if (users.Count() <= 0)
                    {
                        //添加学员信息

                        User user = new User();
                        user.Username = GetString("Username");
                        user.Password = Core.Security.MD5Encrypt(idcard.Substring(idcard.Length - 6, 6));
                        user.Gender = GetString("Gender");
                        user.Nation = GetString("Nation");
                        user.Politics = GetString("Politics");
                        user.IDCard = GetString("IDCard");
                        user.Birthday = GetDate("Birthday");
                        if (user.Birthday < new DateTime(1900, 1, 1))
                        { user.Birthday = DateTime.Today; }
                        user.Telephone = GetString("Telephone");
                        user.Cellphone = GetString("Cellphone");
                        user.WorkingOrgName = GetString("WorkingOrgName");
                        user.Position = GetString("Position");
                        user.OrgID = org.OrgID;

                        HttpPostedFileBase photo = this.HttpContext.Request.Files["photo"];
                        int lenght = photo.ContentLength;
                        if (lenght > 0)
                        {
                            byte[] PhotoArray = new byte[lenght];
                            Stream PhotoStream = photo.InputStream;
                            PhotoStream.Read(PhotoArray, 0, lenght);
                            user.Photo = PhotoArray;
                        }
                        else
                        {
                            //设置默认头像
                            byte[] PhotoArray = new byte[10000];
                            string filePath = HttpContext.Request.MapPath("\\content\\new\\header.jpg");
                            Stream PhotoStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                            PhotoStream.Read(PhotoArray, 0, 10000);
                            user.Photo = PhotoArray;
                        }

                        CQGJ.AddToUser(user);
                        CQGJ.SaveChanges();
                        return RedirectToAction("userlist/" + org.OrgID + "/1");
                    }
                    else
                    {
                        ViewData["ErrorInfo"] = "用户名已存在!";
                    }
                }
                else
                {
                    ViewData["ErrorInfo"] = "请检查用户名和身份证号码是否正确!";
                }
            }
            viewData.Nation = Nation("汉族");
            viewData.GenderList = GenderList("男");
            return View(viewData);
        }