首页 > Web开发 > 详细

EF+MVC+Bootstrap 项目实践 Day4

时间:2015-12-13 00:37:27      阅读:318      评论:0      收藏:0      [点我收藏+]

技术分享

一、注册账户时显示注册表单

非常简单的一句js,但平时用习惯了jquery,原生的还不会写了。。。而且Razor的转成html也折腾了下

<a class="a" href="#" onclick="document.getElementById(‘RegisterForm‘).style.display=‘block‘">注册账户</a>
Html.BeginForm("Index","Auth",FormMethod.Post,new {@id = "RegisterForm",@style = "display:none;"})

平时要显示,就是要把css的"display:none;"改成"display:block;",都是用jquery,$("#xx").show(),最多$("#xx").css("display","block"),甚至$("#xx").attr("style","display:block;")

原生的还以为是.style="display:block;",发现不对,再试了style.display="block;",也不行,分号不能多加。

Html.BeginForm,属性参数前面要加@,这个也折腾了一会

 

二、验证码

源码中验证码比较复杂,有十几种样式,还有各种公共方法基类什么的,我练习就不弄这么复杂了,去掉所有未使用的(VS或Resharper有这功能,没用到的会变灰色,就可以删除)

        [AuthorizeIgnore]
        public ActionResult VerifyImage()
        {
            var validateCodeHelper = new ValidateCodeHelper();
            string code;
            byte[] bytes = validateCodeHelper.CreateImage(out code);
            //this.CookieContext.VerifyCodeGuid = VerifyCodeHelper.SaveVerifyCode(code);
            return File(bytes, @"image/jpeg");
        }
public class ValidateCodeHelper
    {
        public Color[] DrawColors{get;set;} = {
            Color.FromArgb(0x6b,0x42,0x26),
            Color.FromArgb(0x4f,0x2f,0x4f),
            Color.FromArgb(50,0x99,0xcc),
            Color.FromArgb(0xcd,0x7f,50),
            Color.FromArgb(0x23,0x23,0x8e),
            Color.FromArgb(0x70,0xdb,0x93),
            Color.Red,
            Color.FromArgb(0xbc,0x8f,0x8e)
        };
        private bool FontTextRenderingHint{get;set;}
        public int ImageHeight{get;set;} = 30;
        public int ValidataCodeLength{get;set;} = 4;
        public int ValidataCodeSize{get;set;} = 0x10;
        public string ValidateCodeFont{get;set;} = "Arial";

        public byte[] CreateImage(out string validataCode)
        {
            Bitmap bitmap;
            string formatString = "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";
            GetRandom(formatString,ValidataCodeLength,out validataCode);
            MemoryStream stream = new MemoryStream();
            ImageBmp(out bitmap,validataCode);
            bitmap.Save(stream,ImageFormat.Png);
            bitmap.Dispose();
            stream.Close();
            stream.Dispose();
            return stream.GetBuffer();
        }

        private void CreateImageBmp(ref Bitmap bitMap,string validateCode)
        {
            Graphics graphics = Graphics.FromImage(bitMap);
            Random random = new Random();
            graphics.TextRenderingHint = FontTextRenderingHint ? TextRenderingHint.SingleBitPerPixel : TextRenderingHint.AntiAlias;
            Font font = new Font(ValidateCodeFont,ValidataCodeSize,FontStyle.Regular);
            int maxValue = Math.Max((ImageHeight - ValidataCodeSize) - 5,0);
            for(int i = 0; i < ValidataCodeLength; i++){
                Color color = DrawColors[random.Next(DrawColors.Length)];
                Brush brush = new SolidBrush(color);
                int[] numArray = {((i*ValidataCodeSize) + random.Next(1)) + 3,random.Next(maxValue) - 4};
                Point point = new Point(numArray[0],numArray[1]);
                graphics.DrawString(validateCode[i].ToString(),font,brush,point);
            }
            graphics.Dispose();
        }

        private void DisposeImageBmp(ref Bitmap bitmap)
        {
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            Point[] pointArray = new Point[2];
            Random random = new Random();
            for(int i = 0; i < (ValidataCodeLength*2); i++){
                Pen pen = new Pen(DrawColors[random.Next(DrawColors.Length)],1f);
                pointArray[0] = new Point(random.Next(bitmap.Width),random.Next(bitmap.Height));
                pointArray[1] = new Point(random.Next(bitmap.Width),random.Next(bitmap.Height));
                graphics.DrawLine(pen,pointArray[0],pointArray[1]);
            }
            graphics.Dispose();
        }

        private static void GetRandom(string formatString,int len,out string codeString)
        {
            codeString = string.Empty;
            string[] strArray = formatString.Split(,);
            Random random = new Random();
            for(int i = 0; i < len; i++){
                int index = random.Next(0x186a0)%strArray.Length;
                codeString = codeString + strArray[index];
            }
        }

        private void ImageBmp(out Bitmap bitMap,string validataCode)
        {
            int width = (int) ((ValidataCodeLength*ValidataCodeSize)*1.2);
            bitMap = new Bitmap(width,ImageHeight);
            DisposeImageBmp(ref bitMap);
            CreateImageBmp(ref bitMap,validataCode);
        }
    }

就是绘制随机4位英文,具体没细看,代码不算长。

 

三、登陆、新建

验证码还只是展示在页面上,登陆时还要对应起来,

//this.CookieContext.VerifyCodeGuid = VerifyCodeHelper.SaveVerifyCode(code);

这行注释掉的,应该就是要对照的。

 

待续

EF+MVC+Bootstrap 项目实践 Day4

原文:http://www.cnblogs.com/liuyouying/p/5042042.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!