Browse Source

完成注册、验证码登

gaga 7 years ago
parent
commit
700c9826cd

BIN
.vs/PersonalBlog/v15/.suo


BIN
.vs/PersonalBlog/v15/Server/sqlite3/storage.ide


BIN
.vs/PersonalBlog/v15/Server/sqlite3/storage.ide-shm


BIN
.vs/PersonalBlog/v15/Server/sqlite3/storage.ide-wal


+ 21 - 0
PersonalBlog/App_Code/BlogDbContext.cs

@@ -0,0 +1,21 @@
+using PersonalBlog.Models;
+using System;
+using System.Collections.Generic;
+using System.Data.Entity;
+using System.Linq;
+using System.Web;
+
+namespace PersonalBlog.App_Code
+{
+    public class BlogDbContext : DbContext
+    {
+        public BlogDbContext():base("name = dblink")
+        {
+
+        }
+
+
+        public DbSet<User> Users { get; set; }
+
+    }
+}

+ 269 - 0
PersonalBlog/App_Code/ValidateCode.cs

@@ -1,5 +1,7 @@
 using System;
 using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
 using System.Linq;
 using System.Web;
 
@@ -7,6 +9,273 @@ namespace PersonalBlog.App_Code
 {
     public class ValidateCode
     {
+        #region 使用实例
+        //ValidateCode v = new ValidateCode();
+        //string code = v.CreateVerifyCode();            //取随机码    
+        //v.CreateImageOnPage(code, this.Context);       // 输出图片    
+        //Session ["CheckCode"] = code; 
+        #endregion
 
+        #region  验证码长度(默认6个验证码的长度)  
+        int length = 4;
+        public int Length
+        {
+            get { return length; }
+            set { length = value; }
+        }
+        #endregion
+
+        #region 验证码字体大小(为了显示扭曲效果,默认40像素,可以自行修改)  
+        int fontSize = 40;
+        public int FontSize
+        {
+            get { return fontSize; }
+            set { fontSize = value; }
+        }
+        #endregion
+
+        #region 边框补(默认1像素)  
+        int padding = 2;
+        public int Padding
+        {
+            get { return padding; }
+            set { padding = value; }
+        }
+        #endregion
+
+        #region 是否输出燥点(默认不输出)  
+        bool chaos = true;
+        public bool Chaos
+        {
+            get { return chaos; }
+            set { chaos = value; }
+        }
+        #endregion
+
+        #region 输出燥点的颜色(默认灰色)  
+        Color chaosColor = Color.LightGray;
+        public Color ChaosColor
+        {
+            get { return chaosColor; }
+            set { chaosColor = value; }
+        }
+        #endregion
+
+        #region 自定义背景色(默认白色)  
+        Color backgroundColor = Color.White;
+        public Color BackgroundColor
+        {
+            get { return backgroundColor; }
+            set { backgroundColor = value; }
+        }
+        #endregion
+
+        #region 自定义随机颜色数组  
+        Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
+        public Color[] Colors
+        {
+            get { return colors; }
+            set { colors = value; }
+        }
+        #endregion
+
+        #region 自定义字体数组  
+        string[] fonts = { "Arial", "Georgia" };
+        public string[] Fonts
+        {
+            get { return fonts; }
+            set { fonts = value; }
+        }
+        #endregion
+
+        #region 自定义随机码字符串序列(使用逗号分隔)  
+        string codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
+        public string CodeSerial
+        {
+            get { return codeSerial; }
+            set { codeSerial = value; }
+        }
+        #endregion
+
+        #region 产生波形滤镜效果  
+
+        private const double PI = 3.1415926535897932384626433832795;
+        private const double PI2 = 6.283185307179586476925286766559;
+
+        /// <summary>    
+        /// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)    
+        /// </summary>    
+        /// <param name="srcBmp">图片路径</param>    
+        /// <param name="bXDir">如果扭曲则选择为True</param>    
+        /// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>    
+        /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>    
+        /// <returns></returns>    
+        public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
+        {
+            System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
+            // 将位图背景填充为白色     
+            System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
+            graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
+            graph.Dispose();
+
+            double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
+
+            for (int i = 0; i < destBmp.Width; i++)
+            {
+                for (int j = 0; j < destBmp.Height; j++)
+                {
+                    double dx = 0;
+                    dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
+                    dx += dPhase;
+                    double dy = Math.Sin(dx);
+
+                    // 取得当前点的颜色    
+                    int nOldX = 0, nOldY = 0;
+                    nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
+                    nOldY = bXDir ? j : j + (int)(dy * dMultValue);
+
+                    System.Drawing.Color color = srcBmp.GetPixel(i, j);
+                    if (nOldX >= 0 && nOldX < destBmp.Width
+                     && nOldY >= 0 && nOldY < destBmp.Height)
+                    {
+                        destBmp.SetPixel(nOldX, nOldY, color);
+                    }
+                }
+            }
+
+            return destBmp;
+        }
+
+
+
+        #endregion
+
+        #region 生成校验码图片  
+        public Bitmap CreateImageCode(string code)
+        {
+            int fSize = FontSize;
+            int fWidth = fSize + Padding;
+
+            int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
+            int imageHeight = fSize * 2 + Padding;
+
+            System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
+
+            Graphics g = Graphics.FromImage(image);
+
+            g.Clear(BackgroundColor);
+
+            Random rand = new Random();
+
+            //给背景添加随机生成的燥点    
+            if (this.Chaos)
+            {
+
+                Pen pen = new Pen(ChaosColor, 0);
+                int c = Length * 10;
+
+                for (int i = 0; i < c; i++)
+                {
+                    int x = rand.Next(image.Width);
+                    int y = rand.Next(image.Height);
+
+                    g.DrawRectangle(pen, x, y, 1, 1);
+                }
+            }
+
+            int left = 0, top = 0, top1 = 1, top2 = 1;
+
+            int n1 = (imageHeight - FontSize - Padding * 2);
+            int n2 = n1 / 4;
+            top1 = n2;
+            top2 = n2 * 2;
+
+            Font f;
+            Brush b;
+
+            int cindex, findex;
+
+            //随机字体和颜色的验证码字符    
+            for (int i = 0; i < code.Length; i++)
+            {
+                cindex = rand.Next(Colors.Length - 1);
+                findex = rand.Next(Fonts.Length - 1);
+
+                f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
+                b = new System.Drawing.SolidBrush(Colors[cindex]);
+
+                if (i % 2 == 1)
+                {
+                    top = top2;
+                }
+                else
+                {
+                    top = top1;
+                }
+
+                left = i * fWidth;
+
+                g.DrawString(code.Substring(i, 1), f, b, left, top);
+            }
+
+            //画一个边框 边框颜色为Color.Gainsboro    
+            g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
+            g.Dispose();
+
+            //产生波形(Add By 51aspx.com)    
+            image = TwistImage(image, true, 8, 4);
+
+            return image;
+        }
+        #endregion
+
+        #region 将创建好的图片输出到页面  
+        public void CreateImageOnPage(string code, HttpContext context)
+        {
+            System.IO.MemoryStream ms = new System.IO.MemoryStream();
+            Bitmap image = this.CreateImageCode(code);
+
+            image.Save(ms, ImageFormat.Jpeg);
+            context.Response.ClearContent();
+            context.Response.ContentType = "image/Jpeg";
+            context.Response.BinaryWrite(ms.GetBuffer());
+
+            ms.Close();
+            ms = null;
+            image.Dispose();
+            image = null;
+        }
+        #endregion
+
+        #region 生成随机字符码  
+        public string CreateVerifyCode(int codeLen)
+        {
+            if (codeLen == 0)
+            {
+                codeLen = Length;
+            }
+
+            string[] arr = CodeSerial.Split(',');
+
+            string code = "";
+
+            int randValue = -1;
+
+            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
+
+            for (int i = 0; i < codeLen; i++)
+            {
+                randValue = rand.Next(0, arr.Length - 1);
+
+                code += arr[randValue];
+            }
+
+            return code;
+        }
+        public string CreateVerifyCode()
+        {
+            return CreateVerifyCode(0);
+        }
+        #endregion
     }
 }

+ 1 - 1
PersonalBlog/App_Start/RouteConfig.cs

@@ -16,7 +16,7 @@ namespace PersonalBlog
             routes.MapRoute(
                 name: "Default",
                 url: "{controller}/{action}/{id}",
-                defaults: new { controller = "Manage", action = "Index", id = UrlParameter.Optional }
+                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
             );
         }
     }

+ 13 - 1
PersonalBlog/Controllers/HomeController.cs

@@ -1,5 +1,7 @@
-using System;
+using PersonalBlog.App_Code;
+using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;
@@ -18,5 +20,15 @@ namespace PersonalBlog.Controllers
         {
             return View();
         }
+
+        public FileResult ValidCode()
+        {
+            ValidateCode __validateCode = new ValidateCode();
+            string code = __validateCode.CreateVerifyCode();
+            Session["validateCode"] = code;
+            Bitmap __bm = __validateCode.CreateImageCode(code);
+            byte[] bytes = Tools.ConvertHelper.BitmapToBytes(__bm);
+            return File(bytes, @"image/Jpeg");
+        }
     }
 }

+ 75 - 1
PersonalBlog/Controllers/ManageController.cs

@@ -1,4 +1,6 @@
-using System;
+using PersonalBlog.Models;
+using PersonalBlog.ViewModels;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
@@ -8,20 +10,92 @@ namespace PersonalBlog.Controllers
 {
     public class ManageController : Controller
     {
+        private App_Code.BlogDbContext _dbContext = new App_Code.BlogDbContext();
         // GET: Manage
         public ActionResult Index()
         {
             return View();
         }
 
+        [HttpGet]
         public ActionResult Register()
         {
             return View();
         }
 
+        //public ActionResult Register(string UserName, string pwd,string validCode)
+        [HttpPost]
+        public ActionResult Register(RegisterUsers model)
+        {
+            if (ModelState.IsValid)
+            {
+                string sessionValidCode = Session["validateCode"] == null ? string.Empty : Session["validateCode"].ToString().ToLower();
+
+                if (!model.ValidCode.ToLower().Equals(sessionValidCode))
+                {
+                    return Content("验证码不对");
+                }
+                var user = new User();
+                user.AddTime = DateTime.Now;
+                user.UserName = model.UserName;
+                user.Pwd = model.Pwd;//经过md5加密
+                user.Status = true;
+                int i = 0;
+                using (App_Code.BlogDbContext _dbContext = new App_Code.BlogDbContext())
+                {
+                    _dbContext.Users.Add(user);
+                    i = _dbContext.SaveChanges();//保存到数据库
+                }
+                if (i > 0)
+                {
+                    return Redirect("/");
+                }
+                else
+                {
+                    return Content("注册失败");
+                }
+            }
+            else
+            {
+                return Content("验证失败");
+            }
+        }
+        
+        [HttpGet]
         public ActionResult Login()
         {
             return View();
         }
+
+        [HttpPost]
+        public ActionResult Login(LoginUser model)
+        {
+            if (ModelState.IsValid)
+            {
+                string sessionValidCode = Session["validateCode"] == null ? string.Empty : Session["validateCode"].ToString().ToLower();
+
+                if (!model.ValidCode.ToLower().Equals(sessionValidCode))
+                {
+                    return Content("验证码不对");
+                }
+                //根据用户名查找实体
+                using (App_Code.BlogDbContext _dbContext = new App_Code.BlogDbContext())
+                {
+                    var nameUser = _dbContext.Users.FirstOrDefault(m => m.UserName == model.UserName);
+                    if (nameUser == null)
+                    {
+                        return Content("账号或密码不对!");//不要提示太明显
+                    }
+                    else
+                    {
+                         
+                    }
+                }
+            }
+            else
+            {
+                return Content("验证失败");
+            }
+        }
     }
 }

+ 6 - 1
PersonalBlog/Global.asax.cs

@@ -1,4 +1,5 @@
-using System;
+using PersonalBlog.App_Code;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
@@ -16,6 +17,10 @@ namespace PersonalBlog
             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
             RouteConfig.RegisterRoutes(RouteTable.Routes);
             BundleConfig.RegisterBundles(BundleTable.Bundles);
+            using (var blogDbContext =new BlogDbContext())
+            {
+                blogDbContext.Database.CreateIfNotExists();//如果不存在则创建
+            }
         }
     }
 }

+ 26 - 0
PersonalBlog/Models/User.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+using System.Linq;
+using System.Web;
+
+namespace PersonalBlog.Models
+{
+    public class User
+    {
+        [Key]
+        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  //设置自增
+        public int Id { get; set; }
+
+        [StringLength(maximumLength:16,MinimumLength =2)]
+        public string UserName { get; set; }
+
+        [StringLength(maximumLength: 16, MinimumLength = 2)]
+        public string Pwd { get; set; }
+
+        public DateTime AddTime { get; set; }
+
+        public bool Status { get; set; }
+    }
+}

+ 6 - 2
PersonalBlog/PersonalBlog.csproj

@@ -123,7 +123,8 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
-    <Content Include="App_Code\ValidateCode.cs" />
+    <Compile Include="App_Code\BlogDbContext.cs" />
+    <Compile Include="App_Code\ValidateCode.cs" />
     <Compile Include="App_Start\BundleConfig.cs" />
     <Compile Include="App_Start\FilterConfig.cs" />
     <Compile Include="App_Start\RouteConfig.cs" />
@@ -132,7 +133,11 @@
     <Compile Include="Global.asax.cs">
       <DependentUpon>Global.asax</DependentUpon>
     </Compile>
+    <Compile Include="Models\User.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Tools\ConvertHelper.cs" />
+    <Compile Include="ViewModels\LoginUser.cs" />
+    <Compile Include="ViewModels\RegisterUsers.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="favicon.ico" />
@@ -576,7 +581,6 @@
     <Content Include="Views\Shared\_SideBar.cshtml" />
   </ItemGroup>
   <ItemGroup>
-    <Folder Include="Models\" />
     <Folder Include="Views\Account\" />
   </ItemGroup>
   <ItemGroup>

+ 245 - 0
PersonalBlog/Tools/ConvertHelper.cs

@@ -0,0 +1,245 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Web;
+
+namespace PersonalBlog.Tools
+{
+    /// <summary>
+    /// 处理数据类型转换,数制转换、编码转换相关的类
+    /// </summary>
+    public class ConvertHelper
+    {
+        #region 补足位数
+        /// <summary>
+        /// 指定字符串的固定长度,如果字符串小于固定长度,
+        /// 则在字符串的前面补足零,可设置的固定长度最大为9位
+        /// </summary>
+        /// <param name="text">原始字符串</param>
+        /// <param name="limitedLength">字符串的固定长度</param>
+        public static string RepairZero(string text, int limitedLength)
+        {
+            //补足0的字符串
+            string temp = "";
+
+            //补足0
+            for (int i = 0; i < limitedLength - text.Length; i++)
+            {
+                temp += "0";
+            }
+
+            //连接text
+            temp += text;
+
+            //返回补足0的字符串
+            return temp;
+        }
+        #endregion
+
+        #region 各进制数间转换
+        /// <summary>
+        /// 实现各进制数间的转换。ConvertBase("15",10,16)表示将十进制数15转换为16进制的数。
+        /// </summary>
+        /// <param name="value">要转换的值,即原值</param>
+        /// <param name="from">原值的进制,只能是2,8,10,16四个值。</param>
+        /// <param name="to">要转换到的目标进制,只能是2,8,10,16四个值。</param>
+        public static string ConvertBase(string value, int from, int to)
+        {
+            try
+            {
+                int intValue = Convert.ToInt32(value, from);  //先转成10进制
+                string result = Convert.ToString(intValue, to);  //再转成目标进制
+                if (to == 2)
+                {
+                    int resultLength = result.Length;  //获取二进制的长度
+                    switch (resultLength)
+                    {
+                        case 7:
+                            result = "0" + result;
+                            break;
+                        case 6:
+                            result = "00" + result;
+                            break;
+                        case 5:
+                            result = "000" + result;
+                            break;
+                        case 4:
+                            result = "0000" + result;
+                            break;
+                        case 3:
+                            result = "00000" + result;
+                            break;
+                    }
+                }
+                return result;
+            }
+            catch
+            {
+
+                //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
+                return "0";
+            }
+        }
+        #endregion
+
+        #region 使用指定字符集将string转换成byte[]
+        /// <summary>
+        /// 使用指定字符集将string转换成byte[]
+        /// </summary>
+        /// <param name="text">要转换的字符串</param>
+        /// <param name="encoding">字符编码</param>
+        public static byte[] StringToBytes(string text, Encoding encoding)
+        {
+            return encoding.GetBytes(text);
+        }
+        #endregion
+
+        #region 使用指定字符集将byte[]转换成string
+        /// <summary>
+        /// 使用指定字符集将byte[]转换成string
+        /// </summary>
+        /// <param name="bytes">要转换的字节数组</param>
+        /// <param name="encoding">字符编码</param>
+        public static string BytesToString(byte[] bytes, Encoding encoding)
+        {
+            return encoding.GetString(bytes);
+        }
+        #endregion
+
+        #region 将byte[]转换成int
+        /// <summary>
+        /// 将byte[]转换成int
+        /// </summary>
+        /// <param name="data">需要转换成整数的byte数组</param>
+        public static int BytesToInt32(byte[] data)
+        {
+            //如果传入的字节数组长度小于4,则返回0
+            if (data.Length < 4)
+            {
+                return 0;
+            }
+
+            //定义要返回的整数
+            int num = 0;
+
+            //如果传入的字节数组长度大于4,需要进行处理
+            if (data.Length >= 4)
+            {
+                //创建一个临时缓冲区
+                byte[] tempBuffer = new byte[4];
+
+                //将传入的字节数组的前4个字节复制到临时缓冲区
+                Buffer.BlockCopy(data, 0, tempBuffer, 0, 4);
+
+                //将临时缓冲区的值转换成整数,并赋给num
+                num = BitConverter.ToInt32(tempBuffer, 0);
+            }
+
+            //返回整数
+            return num;
+        }
+        #endregion
+
+        #region Bitmap 和 byte[] 互相转换
+        /// <summary>
+        /// byte[] 转换 Bitmap  
+        /// </summary>
+        /// <param name="Bytes"></param>
+        /// <returns></returns>
+        public static Bitmap BytesToBitmap(byte[] Bytes)
+        {
+            MemoryStream stream = null;
+            try
+            {
+                stream = new MemoryStream(Bytes);
+                return new Bitmap((Image)new Bitmap(stream));
+            }
+            catch (ArgumentNullException ex)
+            {
+                throw ex;
+            }
+            catch (ArgumentException ex)
+            {
+                throw ex;
+            }
+            finally
+            {
+                stream.Close();
+            }
+        }
+
+        /// <summary>
+        /// Bitmap转byte[]    
+        /// </summary>
+        /// <param name="Bitmap"></param>
+        /// <returns></returns>
+        public static byte[] BitmapToBytes(Bitmap bitmap)
+        {
+            MemoryStream ms = null;
+            try
+            {
+                ms = new MemoryStream();
+                bitmap.Save(ms, ImageFormat.Jpeg);
+                byte[] byteImage = new Byte[ms.Length];
+                byteImage = ms.ToArray();
+                return byteImage;
+            }
+            catch (ArgumentNullException ex)
+            {
+                throw ex;
+            }
+            finally
+            {
+                ms.Close();
+            }
+        }
+        #endregion
+
+        /// <summary>
+        /// byte[]转换成Image
+        /// </summary>
+        /// <param name="byteArrayIn">二进制图片流</param>
+        /// <returns>Image</returns>
+        public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
+        {
+            if (byteArrayIn == null)
+                return null;
+            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
+            {
+                System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
+                ms.Flush();
+                return returnImage;
+            }
+        }
+
+        /// <summary>
+        /// 将图片Image转换成Byte[]
+        /// </summary>
+        /// <param name="Image">image对象</param>
+        /// <param name="imageFormat">后缀名</param>
+        /// <returns></returns>
+        public static byte[] ImageToBytes(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
+        {
+            if (Image == null) { return null; }
+            byte[] data = null;
+            using (MemoryStream ms = new MemoryStream())
+            {
+                using (Bitmap Bitmap = new Bitmap(Image))
+                {
+
+                    Bitmap.Save(ms, imageFormat);
+                    ms.Position = 0;
+                    data = new byte[ms.Length];
+                    ms.Read(data, 0, Convert.ToInt32(ms.Length));
+                    ms.Flush();
+                }
+            }
+            return data;
+        }
+
+    }
+}

+ 22 - 0
PersonalBlog/ViewModels/LoginUser.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Web;
+
+namespace PersonalBlog.ViewModels
+{
+    public class LoginUser
+    {
+        [Required]
+        [StringLength(maximumLength: 16, MinimumLength = 2)]
+        public string UserName { get; set; }
+
+        [StringLength(maximumLength: 16, MinimumLength = 2)]
+        public string Pwd { get; set; }
+
+        [Required]
+        [StringLength(4)]
+        public string ValidCode { get; set; }
+    }
+}

+ 27 - 0
PersonalBlog/ViewModels/RegisterUsers.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Web;
+
+//此命名空间下的模型为视图模型
+namespace PersonalBlog.ViewModels
+{
+    public class RegisterUsers
+    {
+        [Required]
+        [StringLength(maximumLength: 16, MinimumLength = 2)]
+        public string UserName { get; set; }
+
+        [StringLength(maximumLength: 16, MinimumLength = 2)]
+        public string Pwd { get; set; }
+
+        [StringLength(maximumLength: 16, MinimumLength = 2)]
+        [Compare("Pwd")]
+        public string CPwd { get; set; }
+
+        [Required]
+        [StringLength(4)]
+        public string ValidCode { get; set; }
+    }
+}

+ 36 - 26
PersonalBlog/Views/Manage/Login.cshtml

@@ -1,4 +1,4 @@
-
+@model PersonalBlog.ViewModels.LoginUser
 @{
     Layout = null;
 }
@@ -188,32 +188,42 @@
             <DIV class="initial_right_hand" id="right_hand"></DIV>
         </DIV>
 
-        <P style="padding: 30px 0px 10px; position: relative;">
-            <SPAN class="u_logo"></SPAN>         <INPUT class="ipt" type="text" placeholder="请输入用户名或邮箱" value="">
-        </P>
-        <P style="padding: 0px 0px 10px;position: relative;">
-            <SPAN class="p_logo"></SPAN>
-            <INPUT class="ipt" id="password" type="password" placeholder="请输入密码" value="">
-        </P>
-        <P style="padding: 0px 0px 10px;position: relative;">
-            <SPAN class="p_logo"></SPAN>
-            <INPUT class="iptx" id="password" placeholder="请输入验证码" value="">
-        </P>
-
-        <DIV style="height: 50px; line-height: 50px; margin-top: 30px; border-top-color: rgb(231, 231, 231); border-top-width: 1px; border-top-style: solid;">
-            <P style="margin: 0px 35px 20px 45px;">
-                <SPAN style="float: left;">
-                    <A style="color: rgb(204, 204, 204);"
-                       href="#">忘记密码?</A>
-                </SPAN>
-                <SPAN style="float: right;">
-                    <A style="color: rgb(204, 204, 204); margin-right: 10px;"
-                       href="#">注册</A>
-                    <A style="background: rgb(0, 142, 173); padding: 7px 10px; border-radius: 4px; border: 1px solid rgb(26, 117, 152); border-image: none; color: rgb(255, 255, 255); font-weight: bold;"
-                       href="#">登录</A>
-                </SPAN>
+        @using (Html.BeginForm("Login", "Manage", FormMethod.Post))
+        {
+            <P style="padding: 30px 0px 10px; position: relative;">
+                <SPAN class="u_logo"></SPAN>
+                @*<INPUT name="name" class="ipt" type="text" placeholder="请输入用户名或邮箱" value="">*@
+                @Html.TextBoxFor(m => m.UserName, new { @class = "ipt", placeholder = "请输入用户名或邮箱" })
+                @Html.ValidationMessageFor(m => m.UserName)
             </P>
-        </DIV>
+            <P style="padding: 0px 0px 10px;position: relative;">
+                <SPAN class="p_logo"></SPAN>
+                @*<INPUT name="pwd" class="ipt" id="password" type="password" placeholder="请输入密码" value="">*@
+                @Html.PasswordFor(m => m.Pwd, new { @class = "ipt", placeholder = "请输入密码" })
+                @Html.ValidationMessageFor(m => m.Pwd)
+            </P>
+            <P style="padding: 0px 0px 10px;position: relative;">
+                <SPAN class="p_logo"></SPAN>
+                <INPUT name="validCode" class="iptx" id="password" placeholder="请输入验证码" value="">
+                <img src="/Home/ValidCode" />
+            </P>
+
+            <DIV style="height: 50px; line-height: 50px; margin-top: 30px; border-top-color: rgb(231, 231, 231); border-top-width: 1px; border-top-style: solid;">
+                <P style="margin: 0px 35px 20px 45px;">
+                    <SPAN style="float: left;">
+                        <A style="color: rgb(204, 204, 204);"
+                           href="#">忘记密码?</A>
+                    </SPAN>
+                    <SPAN style="float: right;">
+                        <A style="color: rgb(204, 204, 204); margin-right: 10px;"
+                           href="#">注册</A>
+                        @*<A style="background: rgb(0, 142, 173); padding: 7px 10px; border-radius: 4px; border: 1px solid rgb(26, 117, 152); border-image: none; color: rgb(255, 255, 255); font-weight: bold;"
+                           href="#">登录</A>*@
+                        <input type="submit" value="登录" />
+                    </SPAN>
+                </P>
+            </DIV>
+        }
     </DIV>
 
     <div style="text-align:center;">

+ 46 - 34
PersonalBlog/Views/Manage/Register.cshtml

@@ -1,4 +1,4 @@
-
+@model PersonalBlog.ViewModels.RegisterUsers
 @{
     Layout = null;
 }
@@ -10,6 +10,8 @@
     <meta name="viewport" content="width=device-width" />
     <title>注册</title>
     <script src="~/Include/Scripts/jquery-1.10.2.min.js"></script>
+    <script src="~/Include/Scripts/jquery.validate.min.js"></script>
+    <script src="~/Include/Scripts/jquery.validate.unobtrusive.min.js"></script>
     <STYLE>
         body {
             background: #ebebeb;
@@ -192,40 +194,50 @@
             <DIV class="initial_right_hand" id="right_hand"></DIV>
         </DIV>
 
-        <P style="padding: 30px 0px 10px; position: relative;">
-            <SPAN class="u_logo"></SPAN>         <INPUT class="ipt" type="text" placeholder="请输入用户名或邮箱" value="">
-        </P>
-        <P style="padding: 0px 0px 10px;position: relative;">
-            <SPAN class="p_logo"></SPAN>
-            <INPUT class="ipt" id="password" type="password" placeholder="请输入密码" value="">
-        </P>
-        <P style="padding: 0px 0px 10px;position: relative;">
-            <SPAN class="p_logo"></SPAN>
-            <INPUT class="ipt" id="password" type="password" placeholder="请输入确认密码" value="">
-        </P>
-        <P style="padding: 0px 0px 10px;position: relative;">
-            <SPAN class="p_logo"></SPAN>
-            <INPUT class="iptx" id="password" placeholder="请输入验证码" value="">
-        </P>
-
-        <DIV style="height: 50px; line-height: 50px; margin-top: 30px; border-top-color: rgb(231, 231, 231); border-top-width: 1px; border-top-style: solid;">
-
-            <P style="margin: 0px 35px 20px 45px;">
-                <SPAN style="float: left;">
-                    <A style="color: rgb(204, 204, 204);"
-                       href="#">忘记密码?</A>
-                </SPAN>
-
-                <SPAN style="float: right;">
-                    <A style="color: rgb(204, 204, 204); margin-right: 10px;"
-                       href="#">登录</A>
-
-                    <A style="background: rgb(0, 142, 173); padding: 7px 10px; border-radius: 4px; border: 1px solid rgb(26, 117, 152); border-image: none; color: rgb(255, 255, 255); font-weight: bold;"
-                       href="#">注册</A>
-
-                </SPAN>
+        @*<form action="/manage/Register" id="subform" method="post">*@
+        @using(Html.BeginForm("Register", "Manage", FormMethod.Post))
+        {
+            <P style="padding: 30px 0px 10px; position: relative;">
+                <SPAN class="u_logo"></SPAN>         
+                @*<INPUT name="name" class="ipt" type="text" placeholder="请输入用户名或邮箱" value="">*@
+                @Html.TextBoxFor(m => m.UserName, new { @class = "ipt", placeholder = "请输入用户名或邮箱" })
+                @Html.ValidationMessageFor(m => m.UserName)
             </P>
-        </DIV>
+            <P style="padding: 0px 0px 10px;position: relative;">
+                <SPAN class="p_logo"></SPAN>
+                @*<INPUT name="pwd" class="ipt" id="password" type="password" placeholder="请输入密码" value="">*@
+                @Html.PasswordFor(m => m.Pwd, new { @class = "ipt", placeholder = "请输入密码" })
+                @Html.ValidationMessageFor(m => m.Pwd)
+            </P>
+            <P style="padding: 0px 0px 10px;position: relative;">
+                <SPAN class="p_logo"></SPAN>
+                @*<INPUT class="ipt" id="password" type="password" placeholder="请输入确认密码" value="">*@
+                @Html.PasswordFor(m => m.CPwd, new { @class = "ipt", placeholder = "请输入确认密码" })
+                @Html.ValidationMessageFor(m => m.CPwd)
+            </P>
+            <P style="padding: 0px 0px 10px;position: relative;">
+                <SPAN class="p_logo"></SPAN>
+                <INPUT name="validCode" class="iptx" id="password" placeholder="请输入验证码" value="">
+                <img src="/Home/ValidCode" />
+            </P>
+            <DIV style="height: 50px; line-height: 50px; margin-top: 30px; border-top-color: rgb(231, 231, 231); border-top-width: 1px; border-top-style: solid;">
+                <P style="margin: 0px 35px 20px 45px;">
+                    <SPAN style="float: left;">
+                        <A style="color: rgb(204, 204, 204);"
+                           href="#">忘记密码?</A>
+                    </SPAN>
+                    <SPAN style="float: right;">
+                        <A style="color: rgb(204, 204, 204); margin-right: 10px;"
+                           href="#">登录</A>
+
+                        @*<A style="background: rgb(0, 142, 173); padding: 7px 10px; border-radius: 4px; border: 1px solid rgb(26, 117, 152); border-image: none; color: rgb(255, 255, 255); font-weight: bold;"
+                           href="#" onclick="document.getElementById('subform').submit(); return false">注册</A>*@
+                        <input type="submit"  value="注册"/>
+                    </SPAN>
+                </P>
+            </DIV>
+        }
+        @*</form>*@
     </DIV>
 
     <div style="text-align:center;">

+ 2 - 0
PersonalBlog/Web.config

@@ -10,6 +10,8 @@
   </configSections>
   <connectionStrings>
     <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-PersonalBlog-20171116032327.mdf;Initial Catalog=aspnet-PersonalBlog-20171116032327;Integrated Security=True" providerName="System.Data.SqlClient" />
+    <add name="dblink" connectionString="Data Source=.;Initial Catalog=PersonalBlog;User ID= sa;Password=as;" providerName="System.Data.SqlClient"/>
+    
   </connectionStrings>
   <appSettings>
     <add key="webpages:Version" value="3.0.0.0" />

BIN
PersonalBlog/bin/PersonalBlog.dll


+ 2 - 0
PersonalBlog/bin/PersonalBlog.dll.config

@@ -10,6 +10,8 @@
   </configSections>
   <connectionStrings>
     <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-PersonalBlog-20171116032327.mdf;Initial Catalog=aspnet-PersonalBlog-20171116032327;Integrated Security=True" providerName="System.Data.SqlClient" />
+    <add name="dblink" connectionString="Data Source=.;Initial Catalog=PersonalBlog;User ID= sa;Password=as;" providerName="System.Data.SqlClient"/>
+    
   </connectionStrings>
   <appSettings>
     <add key="webpages:Version" value="3.0.0.0" />

BIN
PersonalBlog/bin/PersonalBlog.pdb


BIN
PersonalBlog/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache


+ 1 - 1
PersonalBlog/obj/Debug/PersonalBlog.csproj.CoreCompileInputs.cache

@@ -1 +1 @@
-8d64c0fe5538d6404cba5353258362c82e992823
+d736f61d2b06408e3b933530cab83254abe06baa

BIN
PersonalBlog/obj/Debug/PersonalBlog.dll


BIN
PersonalBlog/obj/Debug/PersonalBlog.pdb