VS2013中web项目中自动生成的ASP.NET Identity代码思考

70 篇文章 0 订阅

vs2013没有再分webform、mvc、api项目,使用vs2013创建一个web项目模板选MVC,身份验证选个人用户账户。项目会生成ASP.NET Identity的一些代码。这些代码主要在AccountController中。

ASP.NET Identity微软宣称的好处就不说了,这是原文

复制代码
ASP.NET Identity

As the membership story in ASP.NET has evolved over the years, the ASP.NET team has learned a lot from feedback from customers.

The assumption that users will log in by entering a user name and password that they have registered in your own application is no longer valid. The web has become more social. Users are interacting with each other in real time through social channels such as Facebook, Twitter, and other social web sites. Developers want users to be able to log in with their social identities so that they can have a rich experience on their web sites. A modern membership system must enable redirection-based log-ins to authentication providers such as Facebook, Twitter, and others.

As web development evolved, so did the patterns of web development. Unit testing of application code became a core concern for application developers. In 2008 ASP.NET added a new framework based on the Model-View-Controller (MVC) pattern, in part to help developers build unit testable ASP.NET applications. Developers who wanted to unit test their application logic also wanted to be able to do that with the membership system.

Considering these changes in web application development, ASP.NET Identity was developed with the following goals:
• One ASP.NET Identity system •ASP.NET Identity can be used with all of the ASP.NET frameworks, such as ASP.NET MVC, Web Forms, Web Pages, Web API, and SignalR.
• ASP.NET Identity can be used when you are building web, phone, store, or hybrid applications. 

• Ease of plugging in profile data about the user •You have control over the schema of user and profile information. For example, you can easily enable the system to store birth dates entered by users when they register an account in your application.
 Persistence control •By default, the ASP.NET Identity system stores all the user information in a database. ASP.NET Identity uses Entity Framework Code First to implement all of its persistence mechanism.
•Since you control the database schema, common tasks such as changing table names or changing the data type of primary keys is simple to do.
•It's easy to plug in different storage mechanisms such as SharePoint, Windows Azure Storage Table Service, NoSQL databases, etc., without having to throw System.NotImplementedExceptions exceptions.

• Unit testability • ASP.NET Identity makes the web application more unit testable. You can write unit tests for the parts of your application that use ASP.NET Identity.

• Role provider • There is a role provider which lets you restrict access to parts of your application by roles. You can easily create roles such as “Admin” and add users to roles.

• Claims Based • ASP.NET Identity supports claims-based authentication, where the user’s identity is represented as a set of claims. Claims allow developers to be a lot more expressive in describing a user’s identity than roles allow. Whereas role membership is just a boolean (member or non-member), a claim can include rich information about the user’s identity and membership.

•Social Login Providers • You can easily add social log-ins such as Microsoft Account, Facebook, Twitter, Google, and others to your application, and store the user-specific data in your application.

• Windows Azure Active Directory •You can also add log-in functionality using Windows Azure Active Directory, and store the user-specific data in your application. For more information, see  Organizational Accounts in Creating ASP.NET Web Projects in Visual Studio 2013

• OWIN Integration • ASP.NET authentication is now based on OWIN middleware that can be used on any OWIN-based host. ASP.NET Identity does not have any dependency on System.Web. It is a fully compliant OWIN framework and can be used in any OWIN hosted application.
•ASP.NET Identity uses OWIN Authentication for log-in/log-out of users in the web site. This means that instead of using FormsAuthentication to generate the cookie, the application uses OWIN CookieAuthentication to do that.

• NuGet package • ASP.NET Identity is redistributed as a NuGet package which is installed in the ASP.NET MVC, Web Forms and Web API templates that ship with Visual Studio 2013. You can download this NuGet package from the NuGet gallery.
•Releasing ASP.NET Identity as a NuGet package makes it easier for the ASP.NET team to iterate on new features and bug fixes, and deliver these to developers in an agile manner.
复制代码

 

我的感兴趣的主要是几个方面。1、可以扩展用户类的字段;2、使用EF codefirst存储;3、OWIN集成 。4、基于Claims。

我看了整个代码觉得它有点类似于三层架构。这里要特别重要的几个类:

  • ApplicationDbContext类。就是继承字自ef的DbContext,EF codefirst的必备。
  • UserStore类。封装了数据库访问一系列方法,通过DbContext操作数据库。类似三层架构中的数据访问层。
  • UserManager类。用户管理的类,封装了用户、角色、Claim等一系列的方法,通过UserStore类与数据库交互。类似三层的业务逻辑层。
  • AuthenticationManager类。Owin的一些东西,包含了用户登录、注销、验证等一些方法。
  • ApplicationUser。用户模型。继承自IdentityUser,可以自行扩展属性

这个东西怎么用呢:

一、设置ApplicationDbContext类

先看下代码:

复制代码
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }
复制代码

这个类继承自IdentityDbContext类,ApplicationUser是用户模型。

这里要设置的就是base("DefaultConnection")。数据库的链接字符串。在web.config的connectionStrings节设置。

image

我们再分析IdentityDbContext类的元数据。

image

可以看出它继承自DbContext。共再数据库创建两个表,Users根据传输的用户模型来创建。角色表 Roles表根据IdentityRole来创建(这个类只有两个属性ID和name)看以看出这里角色的字段是不能扩展的。

二、扩展用户模型(ApplicationUser)

当然不扩展可以直接用,当预设不满足要求时就可以自行扩展字段。首先看下代码

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
    }

是个继承自IdentityUser的空类,需要扩展的字段直接写这就行,如果扩展个年龄(Age)

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public int Age { get; set; }
    }

我们看下其继承的IdentityUser类,这里面都是一些预知的属性。IUser是一个接口只有ID和username两个属性。

复制代码
public class IdentityUser : IUser
    {
        public IdentityUser();
        public IdentityUser(string userName);

        public virtual ICollection<IdentityUserClaim> Claims { get; }
        public virtual string Id { get; set; }
        public virtual ICollection<IdentityUserLogin> Logins { get; }
        public virtual string PasswordHash { get; set; }
        public virtual ICollection<IdentityUserRole> Roles { get; }
        public virtual string SecurityStamp { get; set; }
        public virtual string UserName { get; set; }
    }
复制代码

三、利用UserManager,AuthenticationManager进行用户相关操作

这一步就可直接在控制器中写代码了。我们先看下AccountController的构造函数

复制代码
[Authorize]
    public class AccountController : Controller
    {
        public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }
复制代码

注意,UserManager,ApplicationUser,UserStore,ApplicationDbContext

1、看下用户注册代码

复制代码
[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);//一个循环向ModelState添加错误消息的方法
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }
复制代码

我们来看下这段代码。

public async Task<ActionResult> Register(RegisterViewModel model)这句将Register声明为异步action。

var user = new ApplicationUser() { UserName = model.UserName };这句是ApplicationUser这个创建模型类。

var result = await UserManager.CreateAsync(user, model.Password); 
这句是一个异步添加用户。利用UserManager这个类的CreateAsync方法创建用户。

类元数据如下图,里面封装了各种用户操作的方法。

image

await SignInAsync(user, isPersistent: false);这句依然。这里是AccountController的一个函数,代码如下:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }

可以看出利用AuthenticationManager的SignOut和SignIn进行清除cookie和登录。

2、再看下注销

复制代码
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }
复制代码

使用AuthenticationManager.SignOut方法。这是类似于 WebFor中的FormsAuthentication所使用的FormsAuthentication.SignOut方法。

3、再看登录代码

复制代码
[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                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);
        }
复制代码

代码比较类似,用UserManagerFindAsync查找用户,成功又有是用调用SignInAsync方法进行登录(方法内部是用AuthenticationManager的SignIn进行登录)

四、总结

这里用到的是ApplicationDbContext、UserStore、UserManager、AuthenticationManager、ApplicationUser这5个类。其中ApplicationDbContext需要设置连接字符串,ApplicationUser可以扩展用户字段,UserStore、UserManager、AuthenticationManager这三个类都封装好的类直接拿来用就行,关键是要理解方法和属性的含义、清楚其用途。

五、问题及思考

  • 依靠[Authorize]应该是用来验证用户是否登录的,直接用来进行权限管理(控制器上写[Authorize(Roles="管理员")])是不是太僵化了?
  • Claims这是东西什么东西,从来没用过,是否与权限有关?
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的教程,帮助您使用 ASP.NET Core 6.0 框架和 SQL Server 数据库生成注册登录界面。 1. 安装 SQL Server 数据库 如果您还没有安装 SQL Server 数据库,请使用此链接下载并安装 SQL Server 数据库:https://www.microsoft.com/en-us/sql-server/sql-server-downloads 2. 创建 ASP.NET Core 6.0 项目 打开 Visual Studio 2022 或更高版本,创建一个新的 ASP.NET Core 6.0 Web 应用程序项目。在创建项目时,选择 Web 应用程序模板。 3. 添加 Entity Framework Core 在解决方案资源管理器,右键单击项目文件夹,选择“管理 NuGet 包”。在 NuGet 包管理器,搜索并安装 Microsoft.EntityFrameworkCore.SqlServer 和 Microsoft.EntityFrameworkCore.Tools 包。 4. 创建数据库模型 在项目根目录,创建一个名为 Models 的文件夹。在该文件夹,创建一个名为 ApplicationUser.cs 的新类。在该类,定义用户模型: ``` using Microsoft.AspNetCore.Identity; namespace YourProjectName.Models { public class ApplicationUser : IdentityUser { // 可以在此定义其他用户属性 } } ``` 5. 创建数据库上下文 在 Models 文件夹创建一个名为 ApplicationDbContext.cs 的新类。在该类,继承 DbContext 并将 ApplicationUser 添加到 DbSet: ``` using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace YourProjectName.Models { public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } } ``` 6. 配置应用程序 在 Startup.cs 文件,配置应用程序以使用 Entity Framework Core 和 SQL Server 数据库。在 ConfigureServices 方法,添加以下代码: ``` services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>(); ``` 在 appsettings.json 文件,添加以下代码: ``` "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true" }, ``` 7. 生成注册和登录功能 在 Visual Studio ,右键单击“Controllers”文件夹,选择“添加” > “新建项目项”。在“添加新项目项”对话框,选择“Identity”模板,并选择“注册”和“登录”选项。单击“添加”按钮,Visual Studio 将为您生成注册和登录功能。 8. 运行应用程序 按 F5 键或单击“调试” > “启动调试”按钮以运行应用程序。您现在可以访问注册和登录页面。 希望这个简单的教程可以帮助到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值