您当前的位置: 首页 >  ide

寒冰屋

暂无认证

  • 0浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

使用内存数据库的.NET Core 3.0中基于身份(Identity-Based)的身份验证

寒冰屋 发布时间:2020-03-08 12:33:22 ,浏览量:0

目录

介绍

背景

先决条件

创建Web应用程序的步骤

第1步

第2步

第3步

第4步

第5步

第6步

运行您的应用程序

总结

  • 下载源9.5 MB
介绍

身份验证是根据系统或用户的身份确定或给予其个人访问权限的过程。.NET Core中有多个选项可以进行身份​​验证。本文演示了如何使用内存数据库在.NET Core 3.0中添加基于身份(Identity-Based)的身份验证。

背景

在上一篇文章中,我介绍了.NET Core 3.0中的Cookie身份验证。

先决条件
  • 从此处安装.NET Core 3.0.0或更高版本的SDK 。
  • 从此处安装最新版本的Visual Studio 2019社区版。
创建Web应用程序的步骤

1、转到Visual Studio 2019,然后选择从选项列表中创建新项目。

2、选择后,将打开一个新窗口以选择项目模板。

3、选择“ASP.NET Core Web应用程序”,然后单击“下一步”按钮。

4、将打开一个新屏幕,以配置新项目。根据您的要求提供项目名称,位置,解决方案名称。按创建按钮。

5、单击“创建”按钮后,将打开一个新屏幕以配置与项目相关的信息,例如您要为Web应用程序创建哪种环境?.NET Framework或.NET Core。从下拉列表中选择.NET Core和ASP.NET Core版本。然后,从列表中选择Web应用程序(模型-视图-控制器)选项,然后按创建按钮创建一个项目。

6、现在,我们的项目将以.NET Core环境的基本结构打开。您可以在解决方案资源管理器中观察到,其中将包含带有“Startup.cs”的Controllers,Models 和Views 文件夹以及其他文件,如下图所示:

7、运行您的应用程序,以检查创建的Web应用程序是否运行正常。默认情况下,它将打开您的项目的主页(Home控制器的Index页)。

安装以下3.0.0版本或最新版本的NuGet

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.InMemo
  • Microsoft.EntityFrameworkCore.Identity.EntityFrameworkCore

集成身份认证

  1. IdentityDbContext:提供管理身份表所需的所有DbSet属性
  2. ApplicationDbContext:用户定义的DbContext类继承自IdentityDbContext类以管理用户身份
  3. UseInMemoryDatabase:将用于标识的用户信息存储在内存中,而不使用任何数据库
  4. AppUser:用户定义的类继承自IdentityUser,以添加用户的其他属性
  5. UserManager:要管理用户信息(如创建用户信息),请删除用户信息
  6. SignInManager:通过HttpContext处理通讯以及用户登录或注销功能
  7. [Authorize]:属性有助于验证访问控制器的用户(用户信息)
第1步

创建一个名为Data的新文件夹并添加ApplicationDbContext 到其中,并将以下代码行放入其中:

using DemoIdentityAuthentication.Models;  
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;  
using Microsoft.EntityFrameworkCore;  
  
namespace DemoIdentityAuthentication.Data  
{  
    // IdentityDbContext contains all the user tables  
    public class ApplicationDbContext : IdentityDbContext  
    {  
        public ApplicationDbContext(DbContextOptions options)  
            : base(options)  
        {  
        }  
    }  
}
第2步

使用名称AppUser将新类添加到Models文件夹中, 并将以下代码行放入其中:

using Microsoft.AspNetCore.Identity;  
  
namespace DemoIdentityAuthentication.Models  
{  
    public class AppUser : IdentityUser  
    {  
        public string Name { get; set; }  
        public string DateOfBirth { get; set; }  
        public string Password { get; set; }  
    }  
}
第3步

Startup.cs文件中的更改:

using DemoIdentityAuthentication.Data;  
using DemoIdentityAuthentication.Models;  
using Microsoft.AspNetCore.Builder;  
using Microsoft.AspNetCore.Hosting;  
using Microsoft.AspNetCore.Identity;  
using Microsoft.EntityFrameworkCore;  
using Microsoft.Extensions.Configuration;  
using Microsoft.Extensions.DependencyInjection;  
using Microsoft.Extensions.Hosting;  
using System;  
  
namespace DemoIdentityAuthentication  
{  
    public class Startup  
    {  
        public Startup(IConfiguration configuration)  
        {  
            Configuration = configuration;  
        }  
  
        public IConfiguration Configuration { get; }  
  
        public void ConfigureServices(IServiceCollection services)  
        {  
            services.AddDbContext(config =>  
            {  
                // for in memory database  
                config.UseInMemoryDatabase("MemoryBaseDataBase");  
            });  
  
            // AddIdentity :-  Registers the services  
            services.AddIdentity(config =>  
            {  
                // User defined password policy settings.  
                config.Password.RequiredLength = 4;  
                config.Password.RequireDigit = false;  
                config.Password.RequireNonAlphanumeric = false;  
                config.Password.RequireUppercase = false;  
            })  
                .AddEntityFrameworkStores()  
                .AddDefaultTokenProviders();  
  
            // Cookie settings   
            services.ConfigureApplicationCookie(config =>  
            {  
                config.Cookie.Name = "DemoProjectCookie";  
                config.LoginPath = "/Home/Login"; // User defined login path  
                config.ExpireTimeSpan = TimeSpan.FromMinutes(5);  
            });   
  
            services.AddControllersWithViews();  
        }  
  
        // This method gets called by the runtime. 
        // Use this method to configure the HTTP request pipeline.  
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
        {  
            if (env.IsDevelopment())  
            {  
                app.UseDeveloperExceptionPage();  
            }  
            else  
            {  
                app.UseExceptionHandler("/Home/Error");  
                // The default HSTS value is 30 days. 
                // You may want to change this for production scenarios, 
                // see https://aka.ms/aspnetcore-hsts.  
                app.UseHsts();  
            }  
            app.UseHttpsRedirection();  
            app.UseStaticFiles();  
  
            app.UseRouting();  
  
            app.UseAuthentication();  
  
            app.UseAuthorization();  
  
            app.UseEndpoints(endpoints =>  
            {  
                endpoints.MapControllerRoute(  
                    name: "default",  
                    pattern: "{controller=Home}/{action=Index}/{id?}");  
            });  
        }  
    }  
}
第4步

 使用新的操作方法进行更新HomeController:

  1. 登录名:用于用户登录
  2. 注册:用于注册新用户
  3. 注销:注销当前用户
  4. 带有Authorize标签的UserInfo:显示有效用户的用户信息,并限制获取无效用户的访问权限

将以下代码行放入HomeController:

using System;  
using System.Collections.Generic;  
using System.Diagnostics;  
using System.Linq;  
using System.Threading.Tasks;  
using Microsoft.AspNetCore.Mvc;  
using Microsoft.Extensions.Logging;  
using DemoIdentityAuthentication.Models;  
using Microsoft.AspNetCore.Identity;  
using Microsoft.AspNetCore.Authorization;  
  
namespace DemoIdentityAuthentication.Controllers  
{  
    public class HomeController : Controller  
    {  
        private readonly UserManager _userManager;  
        private readonly SignInManager _signInManager;  
        public HomeController(  
                   UserManager userManager,  
                   SignInManager signInManager)  
        {  
            _userManager = userManager;  
            _signInManager = signInManager;  
        }  
  
        public IActionResult Index()  
        {  
            return View();  
        }    
  
        [Authorize]  
        public async Task UserInfo()  
        {  
            var user = 
                await _userManager.GetUserAsync(HttpContext.User).ConfigureAwait(false);            
  
            if (user == null)  
            {  
                RedirectToAction("Login");  
            }  
            //login functionality  
  
            return View(user);  
        }  
  
        [HttpGet]  
        public IActionResult Login()  
        {  
            return View();  
        }  
  
        [HttpPost]  
        public async Task Login(AppUser appUser)  
        {  
  
            //login functionality  
            var user = await _userManager.FindByNameAsync(appUser.UserName);  
  
            if (user != null)  
            {  
                //sign in  
                var signInResult = await _signInManager.PasswordSignInAsync
                                   (user, appUser.Password, false, false);  
  
                if (signInResult.Succeeded)  
                {  
                    return RedirectToAction("Index");  
                }  
            }  
  
            return RedirectToAction("Register");  
        }  
  
        public IActionResult Register()  
        {  
            return View();  
        }  
  
        [HttpPost]  
        public async Task Register(AppUser appUser)
        {  
            //register functionality  
  
            var user = new AppUser  
            {  
                Id = "101",  
                UserName = appUser.UserName,  
                Email = appUser.Email,  
                Name = appUser.Name,  
                DateOfBirth = appUser.DateOfBirth,  
                Password = appUser.Password  
            };  
  
            var result = await _userManager.CreateAsync(user, user.Password);  
  
  
            if (result.Succeeded)  
            {  
                // User sign  
                // sign in   
                var signInResult = await _signInManager.PasswordSignInAsync
                                   (user, user.Password, false, false);
  
                if (signInResult.Succeeded)  
                {  
                    return RedirectToAction("Index");  
                }  
            }  
  
            return View();  
        }  
  
        public async Task LogOut(string username, string password)
        {  
            await _signInManager.SignOutAsync();  
            return RedirectToAction("Index");  
        }  
    }  
}
第5步

为HomeController添加view :

1、转到Views文件夹,然后选择“Home文件夹”。

2、右键单击Home文件夹以选择添加选项,然后选择视图。

3、将打开一个弹出窗口以添加视图。

4、提供“视图名称”为Login,选择“模板”为Empty,选择“使用布局页面”,然后按“添加”按钮。一个新的Login.cshtml文件将创建到Home文件夹中。请参考下图添加视图:

5、对于其他视图,请遵循相同的步骤,并为UserInfo 操作和Register 操作创建视图。

6、Login.cshtml 页面的代码:

@model DemoIdentityAuthentication.Models.AppUser  
  
User Login  
  
  
    
Username
Password
Login

@model DemoIdentityAuthentication.Models.AppUser  
  
@{  
    ViewData["Title"] = "Register";  
}  
  
User Register  
  
  
    
Name
Email Id
Username
Password
Date Of Birth
Register

7、UserInfo.cshtml页面的代码:

@model DemoIdentityAuthentication.Models.AppUser  
  
User Information  
  
  
      
        Name:-   
        @Html.DisplayFor(model => model.Name)  
      
      
        Email:-   
        @Html.DisplayFor(model => model.Email)  
      
      
        Username:-   
        @Html.DisplayFor(model => model.UserName)  
      
      
        Date Of Birth:-   
        @Html.DisplayFor(model => model.DateOfBirth)  
      
第6步

更新_Layout.cshtml 页面以添加用于用户登录、用户注册、用户信息和用户注销的新选项卡/超链接。

_Layout.cshtml的代码如下:

  
  
  
      
      
    @ViewData["Title"] - DemoIdentityAuthentication  
      
      
  
  
      
          
            
DemoIdentityAuthentication
  • Home
  • Register
  • Login
  • User Information
  • Logout
@RenderBody()
© 2020 - DemoIdentityAuthentication - Privacy
@RenderSection("Scripts", required: false)
运行您的应用程序

成功运行应用程序后,应用程序的输出应类似于以下屏幕:

1、单击“用户信息”选项卡以获取当前的登录用户信息,它将打开一个登录页面以登录用户。问题:为什么会要求登录?答:[Authorize]属性限制访问未经授权的请求的数据/信息,并重定向到登录页面以检查用户是否有效。在我们的示例中,我们在HomeController的UserInformation操作方法上添加了此属性。

2、在登录之前注册您的帐户。单击注册选项卡,然后根据以下屏幕提供用户信息:

3、成功注册后,它将在浏览器中创建一个cookie,如下所示:

4、现在,单击“登录”选项卡,然后输入用户名和密码,如以下屏幕所示:

再次单击UserInformation选项卡,现在您无需登录即可查找用户信息的最终结果。

总结

在本文中,我讨论了如何使用内存数据库在.NET Core 3.0中添加基于身份的身份验证。我们还创建了用户登录和注册表单,以使用户登录到我们的应用程序以访问有用的信息。我希望这将有助于读者理解如何在任何应用程序中实现基于身份的身份验证。请找到随附的代码以更好地理解。

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0450s