mvc登录验证

调用loginAction方法显示登录页面

//登录方法
    function loginAction(){
        require VIEW_PATH.'login.html';
    }

login.html的css js都要改,将form表单的提交地址改为check方法

//表单验证
    function checkAction(){
        $uid = $_POST['uid'];  //form表单传值
        $pwd = $_POST['pwd'];
        $yuangong = Factory::getDx('YuangongModel');
        $arr = $yuangong->check($uid);
        
        if($arr[0] == $pwd && $pwd != ""){
            $_SESSION['is_login'] = 'yes';
            $_SESSION['name'] = $arr[1];
            $this->_jump('new_file.php?p=back&c=Index&a=index',"登陆成功");
        }else{
            $this->_jump('new_file.php?p=back&c=Admin&a=login');
        }
    }

获取用户的账号和密码,调用Factory的getDx方法获得YuangongModel的一个对象,员工模型类的对象调用员工模型类的check方法,执行sql语句返回一维数组,判断密码是否正确,正确则显示登陆成功跳到首页,错误则返回登录页。

 

转载于:https://www.cnblogs.com/liangdong/p/10435165.html

在C# MVC中,可以通过使用ASP.NET Identity来实现登录验证。具体步骤如下: 1. 在Visual Studio中创建一个新的MVC项目。 2. 在NuGet包管理器中安装Microsoft.AspNet.Identity.Core和Microsoft.AspNet.Identity.EntityFramework。 3. 在Models文件夹中创建一个名为ApplicationUser.cs的类,该类继承自IdentityUser类。 4. 在DbContext类中添加以下代码以启用Identity: ``` public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } ``` 5. 在Startup.cs文件中添加以下代码以配置Identity: ``` public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) } }); } ``` 6. 在AccountController.cs文件中添加以下代码以处理登录: ``` [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } var user = await UserManager.FindAsync(model.UserName, model.Password); if (user != null) { await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("", "Invalid username or password."); return View(model); } } ``` 7. 在Views文件夹中创建一个名为Login.cshtml的视图,该视图包含登录表单。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值