设为首页 加入收藏

TOP

asp.net core 系列之用户认证(authentication)(一)
2019-09-17 18:33:36 】 浏览:51
Tags:asp.net core 系列 用户 认证 authentication

 

ASP.NET Core 的 identity 是一种需要用户登录的会员系统,用户可以创建一个登录信息存储在 Identity 的的账号,

或者也可以使用第三方登录,支持的第三方登录包括:Facebook, Google, Microsoft Account, and Twitter.

 

Identity 使用Sql Server 存储用户的姓名,密码等数据,当然你也可以选择其他的存储工具进行存储

 

这篇教程,将会讲解如何使用Identity进行用户的注册,登录,登出

 

1.创建一个带认证(authentication)的web应用

  • 文件->新建->项目
  • 选择ASP.NET Core Web 应用程序,命名WebApp1 ,点击确定
  • 然后选择web 应用程序,然后更改身份验证
  • 选择个人用户账号,确定

 

生成的项目会提供 ASP.NET Core Identity 功能,并且 Identity area 会暴露 下面几个 终端(endpoint):

  • /Identity/Account/Login
  • /Identity/Account/Logout
  • /Identity/Account/Manage

2.迁移

观察生成的代码,发现migration已经生成了,只需要更新到数据库

在nuget 程序控制台中,输入:

Update-Database

 

直接在vs中的视图,打开sql server 对象管理器,查看数据库效果,确认数据库更新成功:

 

 

 

3.配置 Identity 服务(Identity service)

服务被添加到了StartUp下的 ConfigureServices方法中

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity<IdentityUser>()
        .AddDefaultUI(UIFramework.Bootstrap4)
        .AddEntityFrameworkStores<ApplicationDbContext>();

  //这里对Identity做一些配置 services.Configure
<IdentityOptions>(options => { // Password settings.密码配置 options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; // Lockout settings.锁定设置 options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.AllowedForNewUsers = true; // User settings.用户设置 options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); services.ConfigureApplicationCookie(options => { // Cookie settings 缓存设置 options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); options.LoginPath = "/Identity/Account/Login"; options.AccessDeniedPath = "/Identity/Account/AccessDenied"; options.SlidingExpiration = true; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }

 

 4.添加 注册,登录,登录功能

  • 在解决方案的项目上,右键添加->新搭建基架的项目
  • 选择标识,添加
  • 然后选择你想添加的项

 

 

这里的数据上下文中需要选中一个数据的,注意

 

之后,会生成相应的一些文件,包括注册,登录,登出

 

5.现在再看下,生成的代码

注册
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
    returnUrl = returnUrl ?? Url.Content("~/");
    if (ModelState.IsValid)
    {
        var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
        var result = await _userManager.CreateAsync(user, Input.Password); //创建账户
  
     if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmation
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇第13章 Base64 URL编码 - Identit.. 下一篇C#程序代码中常用的快捷键

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目