Exemplo n.º 1
0
        /// <summary>
        /// 新增apiUser
        /// </summary>
        /// <returns></returns>
        public string insertApiUser(insertApiUserObj apiUser)
        {
            account acc            = new account();
            string  aspnetUserId   = "apiUser_" + apiUser.userId.Trim();
            string  aspnetPassword = "******" + apiUser.password.Trim();

            var newUser = new ApplicationUser()
            {
                UserName = aspnetUserId
            };

            // part1.使用account建立User於AspNet.Identity產生的[AspNetUsers]資料表中
            bool bresult = acc.CreateUser(newUser, aspnetPassword);

            // part2.同時建立在自訂的[sys_apiUser]資料表中
            if (bresult)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
                SqlCommand    cmd  = new SqlCommand("INSERT INTO sys_apiUser (userId,company,note,password,issuedOn,expiredOn,lockYN,aspnetUserId,aspnetPassword) Values (@userId,@company,@note,@password,@issuedOn,@expiredOn,@lockYN,@aspnetUserId,@aspnetPassword)", conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@userId", apiUser.userId.Trim());
                cmd.Parameters.AddWithValue("@company", apiUser.unit);
                cmd.Parameters.AddWithValue("@note", apiUser.note);
                cmd.Parameters.AddWithValue("@password", apiUser.password.Trim());
                cmd.Parameters.AddWithValue("@issuedOn", DateTime.Now);
                cmd.Parameters.AddWithValue("@expiredOn", DateTime.Now.AddYears(100));
                cmd.Parameters.AddWithValue("@lockYN", "0");
                cmd.Parameters.AddWithValue("@aspnetUserId", aspnetUserId);
                cmd.Parameters.AddWithValue("@aspnetPassword", aspnetPassword);
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Close(); dr.Dispose(); conn.Close(); conn.Dispose();
                return("ok");
            }
            else
            {
                return("error");
            }
        }