php mvc登陆注册,Asp.Net MVC 5使用Identity之简单的注册和登陆
[导读]由于.Net MVC 5登陆和注册方式有很多种,但是Identity方式去实现或许会更简单更容易理解
由于.Net MVC 5登陆和注册方式有很多种,但是Identity方式去实现或许会更简单更容易理解
首先新建一个项目
其次如下选择Empty和MVC的选项
然后打开NuGet包管理器分别安装几个包
然后往Models文件夹里面添加ApplicationUser类,SignInModel类,SignUpModel类,ApplicationDbContext类,当然ApplicationDbContext类你也可以分到DbContext到另一个类库,我这是做演示用的,分层不用么这么明确
----------------------------------------------------------------------
ApplicationUser类
ApplicationDbContext类
SignInModel类
SignUpModel类
然后往App_Start文件夹里面添加ApplicationSignInManager类,ApplicationUserManager类,ApplicationUserStore类
---------------------------------------------------------------------
ApplicationUserManager类
ApplicationSignInManager类
ApplicationUserStore类
然后往Controller文件夹里面添加HomeController控制器,AccountController控制器
先往HomeController控制器里添加index视图
index视图代码@using Microsoft.AspNet.Identity
@{
ViewBag.Title = "Index";
}
Index
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
Hello @User.Identity.GetUserName()
- Log off
}
}
else
{
@Html.ActionLink("Login", "Login", "Account")
@Html.ActionLink("Register", "Register", "Account")
}
然后AccountController控制器代码private ApplicationSignInManager signInManager;
private ApplicationUserManager userManager;
public ApplicationSignInManager SignInManager
{
get
{
return signInManager "", "登陆无效");
return View(model);
}
}
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task Register(SignUpModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");
}
private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
}
然后分别添加生成Login和Register页面
Login页面代码@model IdentityDemo.Models.SignInModel
@{
ViewBag.Title = "Login";
}
Login
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post))
{
@Html.AntiForgeryToken()
SignInModel
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.TextBoxFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.PasswordFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.RememberMe, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.CheckBoxFor(model => model.RememberMe)
@Html.ValidationMessageFor(model => model.RememberMe, "", new { @class = "text-danger" })
}
@Html.ActionLink("注册", "Register")
Register页面代码@model IdentityDemo.Models.SignUpModel
@{
ViewBag.Title = "Register";
}
Register
@using (Html.BeginForm("Register", "Account", FormMethod.Post))
{
@Html.AntiForgeryToken()
SignUpModel
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.TextBoxFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.PasswordFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.PasswordFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
}
然后往项目的根目录添加Startup类
然后修改根目录的Web.Config文件
最后我们来测试一下看看效果怎么样,如下图
php mvc登陆注册,Asp.Net MVC 5使用Identity之简单的注册和登陆相关推荐
- ASP.NET MVC 5 02 - ASP.NET MVC 1-5 各版本特点
参考书籍:<ASP.NET MVC 4 高级编程>.<ASP.NET MVC 5 高级编程>.<C#高级编程(第8版)>.<使用ASP.NET MVC开发企业 ...
- 学习笔记 --- 工厂、单体、适配器、策略、观察者、MVC设计模式及ASP.NET MVC开发模式、关闭缓存的方法...
关于工厂.单体.适配器.策略.观察者没啥好说的, 代码中有说明 //DesignPattern.cs View Code using System; using System.Collections. ...
- mvc html validator,ASP.NET MVC实现Validation验证器扩展
今天介绍在ASP.NET MVC实现Validation验证器扩展,通过使用Controller验证并不是最好的方法:验证过于分散,容易造成重复代码,不利于维护与扩展,因此本节将使用MVC默认绑定器( ...
- [导入]ASP.NET MVC框架开发系列课程(1):MVC模式与ASP.NET MVC框架概述.zip(8.80 MB)
讲座内容: ASP.NET MVC框架是既ASP.NET WebForms后的又一种开发方式.它提供了一系列优秀特性,使ASP.NET开发人员拥有了另一个选择.本次课程将对MVC模式ASP.NET M ...
- php .net mvc,总是觉得asp.net MVC 写着很别扭,对比PHP的mvc,asp.net 麻烦很多?
没写过php,但有ASP.NET MVC的开发经验,这里简单地说一下自己的理解. 首先通过一张图来看ASP.NET MVC的一个交互原理: Model层其实是一个很大的概念,包含了服务,业务逻辑,数据 ...
- mvc ajax helpers,ASP.NET MVC 实践系列4-Ajax应用
ASP.NET MVC中支持Ajax的方式和webform中有些区别,没有了updatepanel,所以对于初学者来说在最开始应用时似乎没有在webform中简单,但实际使用上更为灵活而跟webfor ...
- asp.net mvc 点滴 一 asp.net mvc vs asp.net web forms
MVC模式是一个大家很熟悉的设计模式,微软的asp.net mvc framework是给asp.net web forms提供了一个可替换的asp.net方案. Models:Model对象是程序的 ...
- asp.net+mvc+html辅助,ASP.NET MVC使用Ajax的辅助的解决方法
前言:前面我们已经简单的介绍过了MVC如何Jquery,因为我们如果使用Ajax的话必须要了解Jquery,这篇博客我们将大致了解一下ASP.NET MVC如何使用Ajax的辅助方法,此博客是我的读书 ...
- mvc html dropdownlist,ASP.NET MVC中使用DropDownList地详解
DropDownList控件又称下拉列表框控件,DropDownList控件动态连接到数据库,按指定 条件从数据库 中查询 出列表选项数据,然后绑 定到控件,可以方便快速地显示出多个下拉选 项 . 同 ...
最新文章
- 学业水平考试b能上985吗_河南单招哪些学院好考?哪些专业能录取?
- NGINX中的proxy_pass和rewrite
- ELK(Logstash+Elasticsearch+Kibana)的原理和详细搭建
- matlab在电气工程中的数值分析
- Android开发--RadioButton和CheckBox控件的使用
- 查询所有的存储过程哪些中包含某个字符串
- redchat怎么编写shell脚本_如何写shell脚本?尝试自己编写一个简单脚本
- C学习杂记(二)笔试题:不使用任何中间变量如何将a、b的值进行交换
- wordpress进阶教程(十九):创建自定义的找回密码页面
- java random产生随机数_Random随机数
- 【VMware混合云】掀起你的盖头来
- 并发编程之美(1)并发编程基础二
- 以太坊Merkle Patricia Tree(MPT)技术深度解读
- QC1.0、QC2.0、QC3.0、QC4.0协议介绍
- 计算机主机与显示屏如何接线,主机跟显示器怎么连接
- 几个炫酷且实用的CSS动画效果
- wxPay微信支付订单提交提示「订单号重复」问题
- 微服务出现Shutting down DiscoveryClient问题
- 斗鱼“亏转盈”,内容付费真的能代替游戏直播?
- Python求最大公倍数
热门文章
- 汇编入门之输入、输出、奇偶判断、多字节变量定义
- Fluent Ribbon项目出现“命名空间“clr-namespace:Fluent;assembly=Fluent”中不存在“RibbonWindow”名称”的解决方法...
- postgreSQL源码分析——索引的建立与使用——GIN索引(3)
- html最小化位置不变,实现DIV相对于浏览器固定位置不变
- 查看usb控制器固件日期_三星发布两款USB Type-C电源控制器芯片 支持100W的充电功率...
- mysql免安装_腾讯云Ubuntu18.04部置Django2系列(二):Ubuntu18.04 安装Mysql
- vim linux python3,VIM:在python-mode中使用python3解释器
- c++的lambda表达式捕获this_贯穿 C++ 11 与 C++ 17 的 Lambda 到底是个什么?
- raid5 合适 多少块硬盘_分析Linux raid6同步成raid5导致数据丢失的情况
- 客户和顾客是一个意思吗_“啤酒度数”和“啤酒酒精度”一个意思吗?