php mvc登陆注册,Asp.Net MVC 5使用Identity之简单的注册和登陆

[导读]由于.Net MVC 5登陆和注册方式有很多种,但是Identity方式去实现或许会更简单更容易理解

由于.Net MVC 5登陆和注册方式有很多种,但是Identity方式去实现或许会更简单更容易理解

首先新建一个项目

4667e439b0f1dca2182a4c084d6407ee.png

其次如下选择Empty和MVC的选项

610ae0d9d8c210812f949cb5ed179793.png

然后打开NuGet包管理器分别安装几个包

448c6e20577a58edc677bd532939407e.png

然后往Models文件夹里面添加ApplicationUser类,SignInModel类,SignUpModel类,ApplicationDbContext类,当然ApplicationDbContext类你也可以分到DbContext到另一个类库,我这是做演示用的,分层不用么这么明确

----------------------------------------------------------------------

ApplicationUser类

dfd843c287abf7a93d1b26492508e992.png

ApplicationDbContext类

2f985c688385c8107bf2dff9c8ca0394.png

SignInModel类

71f89c8014f88b8a12572b6227c745df.png

SignUpModel类

4c91de2c776fb19caa8c79e0fc8c9ead.png

然后往App_Start文件夹里面添加ApplicationSignInManager类,ApplicationUserManager类,ApplicationUserStore类

---------------------------------------------------------------------

ApplicationUserManager类

16c9088c5c02434e163cdfae9ccba7c2.png

ApplicationSignInManager类

49add751dcc2bfecae76a29b46a56562.png

ApplicationUserStore类

4f58a39aeda0270ee54570bd7260e227.png

然后往Controller文件夹里面添加HomeController控制器,AccountController控制器

先往HomeController控制器里添加index视图

bc38f02c16c5a6ea67d4541b3ca4bdb4.png

bd25fd16710bca2b0cc8bdbcbb2d9d7d.png

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")

}

b1248f463a2e70124c1c5c6bf9458b58.png

然后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; }

}

06e11eb28ef9c94575043621e5b4a43a.png

然后分别添加生成Login和Register页面

627810c8a70ad08de1f7bccfd397f373.png

c2eff0aff1005a993f62aa62a02f57f6.png

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")

211eaa92f4157784915fa81594237870.png

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" } })

}

0862e04429d8448e20c58ea38d9b4e8d.png

然后往项目的根目录添加Startup类

03f9b6a6f1b79fae09c5e2fdf1ebe450.png

70793d5a283f1f6b14bbdabcce8bbb4e.png

然后修改根目录的Web.Config文件

65c0afc0ba858895392f6e3e613e7834.png

最后我们来测试一下看看效果怎么样,如下图

1ad6771dcf7e7b813e6be04e1cfedef5.png

a08ded768f97d39e5191fd04ccbf3c5e.png

8ee26798970a762273ca546f41b9b01b.png

1d7ace893fbf455a0e904a00bf6d4e0f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值