2021-11-01


@{
    ViewData["Title"] = "Index";
}
@section css{
    <link rel="stylesheet" type="text/css" href="~/css/Loingys.css" />
    <link rel="stylesheet" type="text/css" href="~/css/Loingwdys.css" />
}
<div id="formContainer" class="dwo">
    <div class="formLeft">
        <img src="~/Publish/Mscdd.png">
    </div>
    <div class="formRight">
        <!-- Forgot password form -->
        <form id="forgot" class="otherForm">
            <header>
                <h1>忘记密码</h1>
                <p>输入邮箱找回密码</p>
            </header>
            <section>
                <label>
                    <span>邮箱</span>
                    <input type="email" placeholder=" ">
                    <span class="border"></span>
                </label>
                <button type="submit">发送邮件</button>
            </section>
            <footer>
                <button type="button" class="forgotBtn">返回</button>
            </footer>
        </form>

        <!-- Login form -->
        <form action="User/Login" method="post">
            <header>
                <h1>欢迎回来</h1>
                <p>请先登录</p>
            </header>
            <section>
                <label>
                    <span style="color:#ffffff">用户名</span>
                    <input type="text" placeholder=" " id="LogName" name="LogName">
                    <span class="border"></span>
                </label>
                <label>
                    <span style="color:#ffffff">密码</span>
                    <input type="password" placeholder=" " id="Password" name="Password">
                    <span class="border"></span>
                </label>
                <div class="addboj">
                    <span style="color:#ffffff">验证码 </span>
                    <input type="text" id="validateCode" class="col-form-label-sm" />
                    <img id="imgVerify" src="~/User/ValidateCode" alt="看不清?点击更换" onclick="this.src = this.src + '?'" style="vertical-align:middle;" />
                    <span class="border"></span>
                </div>
                <br />
                <span id="code" style="color:red;margin-left:100px;"></span>
                <br />
                <br />
                <button type="submit" class="btn btn-primary" onclick="addPush()">登录</button>
            </section>
            <footer>
                <button type="button" class="forgotBtn">忘记密码?</button>
                <button type="button" class="registerBtn">新用户?</button>
            </footer>
        </form>

        <!-- Register form -->
        <form id="register" class="otherForm">  
            <header>
                <h1>用户注册</h1>
                <p>注册后享受更多服务</p>
            </header>
            <section>
                <label>
                    <span>用户名</span>
                    <input type="text" placeholder=" ">
                    <span class="border"></span>
                </label>
                <label>
                    <span>邮箱</span>
                    <input type="email" placeholder=" ">
                    <span class="border"></span>
                </label>
                <label>
                    <span>密码</span>
                    <input type="password" placeholder=" ">
                    <span class="border"></span>
                </label>
                <label>
                    <span>重复密码</span>
                    <input type="password" placeholder=" ">
                    <span class="border"></span>
                </label>
                <button type="submit">注 册</button>
            </section>
            <footer>
                <button type="button" class="registerBtn">返回</button>
            </footer>
        </form>
    </div>
</div>
@section Scripts{
    <script type="text/javascript">
        //表单验证验证验证码是否正确
        $(function () {

        });
        var str = "?";
        function addPush() {
            $.ajax({
                url: 'User/IzUserVoder',
                dataType: 'JSON',
                type: 'POST',
                data: {
                    userVerCode: $("#validateCode").val()
                },
                success: function (date) {
                    if (date == "0") {
                        $("#imgVerify").attr("src", "Home/ValidateCode" + str)
                        $("#validateCode").val("");
                        $("#code").css("color", "red");
                        $("#code").html("验证码不正确");
                        str += str;
                        return;
                    }
                    $("#code").css("color", "#66FF59");
                    $("#code").html("验证码正确");
                }
            })
        }
        //登录注册密码页面切换
        $(function () {
            $('.forgotBtn').click(function () {
                $('#forgot').toggleClass('toggle')
            })

            $('.registerBtn').click(function () {
                $('#register, #formContainer').toggleClass('toggle')
            })
        })
    </script>
}

// //验证方法
        public static string VerificationCodeCacheFormat = "vcode_cache_{0}";
        public IActionResult ValidateCode()
        {
            VerificationCodeServices _vierificationCodeServices = new VerificationCodeServices();
            string code = "";
            System.IO.MemoryStream ms = _vierificationCodeServices.Create(out code);
            code = code.ToLowerInvariant();//验证码不分大小写
            HttpContext.Session.SetString("SupportValidateCode", code);
            Response.Body.Dispose();
            var token = Guid.NewGuid().ToString();
            Response.Cookies.Append("validatecode", token);
            return File(ms.ToArray(), @"image/png");
        }

        public IActionResult IzUserVoder(string userVerCode)
        {
            if (VerifyUserInputCode(userVerCode))
            {
                HttpContext.Session.SetString("SupportValidateCode", "");
                return Json(1);
            }
            return Json(0);
        }

        /// <summary>
        /// 验证码判断
        /// </summary>
        /// <param name="userVerCode"></param>
        /// <returns></returns>
        public bool VerifyUserInputCode(string userVerCode)
        {
            string vCode = HttpContext.Session.GetString("SupportValidateCode");
            if (vCode != userVerCode) return false;
            return true;
        }
        //登录
        public IActionResult Login(string LogName, string Password)
        {
            if (string.IsNullOrEmpty(LogName))
            {
                return RedirectToAction("index");
            }
            if (string.IsNullOrEmpty(Password))
            {
                return RedirectToAction("index");
            }
            SystemUser data = hc.SystemUsers.Where(p => p.LogName == LogName.Trim()
            && p.Password == Password.Trim()).FirstOrDefault();
            if (data == null)
            {
                return RedirectToAction("index");
            }
            HttpContext.Session.SetString("logName", data.LogName);
            return RedirectToAction("list");
        }
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbCore.Model.Models
{ 
    /// <summary>
  /// 图片验证码
  /// </summary>
    public class VerificationCodeServices
    {
        /// <summary>  
        /// 生成指定长度的随机字符串 
        /// </summary>  
        /// <param name="codeLength">字符串的长度</param>  
        /// <returns>返回随机数字符串</returns>  
        private string RndomStr(int codeLength)
        {
            string chars = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,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,P,P,Q,R,S,T,U,V,W,X,Y,Z";

            string[] charArray = chars.Split(new Char[] { ',' });
            string code = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 1; i < codeLength + 1; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(61);
                if (temp == t)
                {
                    return RndomStr(codeLength);
                }
                temp = t;
                code += charArray[t];
            }
            return code;
        }

        /// <summary>  
        /// 将生成的字符串写入图像文件  
        /// </summary>  
        /// <param name="code">验证码字符串</param>
        /// <param name="length">生成位数(默认4位)</param>  
        public MemoryStream Create(out string code, int length = 4)
        {
            code = RndomStr(length);
            Bitmap Img = null;
            Graphics graphics = null;
            MemoryStream ms = null;
            Random random = new Random();
            Color[] color = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
            Img = new Bitmap((int)code.Length * 18, 32);
            graphics = Graphics.FromImage(Img);
            graphics.Clear(Color.White);


            for (int i = 0; i < 100; i++)
            {
                int x = random.Next(Img.Width);
                int y = random.Next(Img.Height);
                graphics.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }

            for (int i = 0; i < code.Length; i++)
            {
                int colorIndex = random.Next(7);
                int fontIndex = random.Next(4);
                Font font = new Font(fonts[fontIndex], 15, FontStyle.Bold);
                Brush brush = new SolidBrush(color[colorIndex]);
                int y = 4;
                if ((i + 1) % 2 == 0)
                {
                    y = 2;
                }
                graphics.DrawString(code.Substring(i, 1), font, brush, 3 + (i * 12), y);
            }
            ms = new MemoryStream();
            Img.Save(ms, ImageFormat.Png);
            graphics.Dispose();
            Img.Dispose();
            return ms;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值