安装 AspNet.Identity 程序包

Microsoft.AspNet.Identity.Core 包含 ASP.NET Identity 核心接口
Microsoft.AspNet.Identity.EntityFramework ASP.NET Identity 的实体框架提供程序

添加用户注册代码

var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore); var user = new IdentityUser() { UserName = ... };
IdentityResult result = manager.Create(user, ...); if (result.Succeeded)
{
}
else
{
}
  • UserStore 实现以下在 ASP.NET Identity Core 中定义的接口:

    • IUserStore,公布用户管理的基本 api
    • IUserLoginStore,此接口将用户映射到 login 提供程序,例如 Google, Facebook, Twitter
    • IUserClaimStore 保存声明。
    • IUserRoleStore  此接口将用户映射到角色
  • IdentityUser 实现在 ASP.NET Identity Core 中定义的 IUser 接口,IUser 接口表示用户。
  • UserManager 公布向 UserStore 保存更改的 API。
  • IdentityResult 在 ASP.NET Identity Core 中定义,表示操作结果。

类间关系如图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsgAAAF1CAIAAAB6dYsXAAAgAElEQVR4nO3d7bGiShMAYOLZEAxhA7E2jFu1CRjBG4ZVG4vB+P7Qo3wMiDBAMzxPUbfOVeSju2do0XO2ugMAZFJtfQAAQDk0FgBANhoLACAbjQUAkI3GAgDIRmMBAGSjsQAAstFYAADZaCwAgGw0FgBANhoLACAbjQUAkI3GAgDIRmMBAGSjsQAAstFYAADZaCwAgGw0FgBANhoLACCbbRqL/6pqL4sIHHmRfdEudRHthSLAfcPG4n7/F39Zrv72EoEjL7Iv2qUuoq23WJTG4kPx/fz3oBE48iL7ol3qItrLRYC7xmJM8S1Rf3uJwJEX2RftUhfRXi4C3DUWI4sve/3tJQJHXmRftEtdRHu5CHCP2VhUK5bm8L5ax5mx/gYiMHxIrWfnx2pgCx/3VdXUV6ivWfXo29fAaq299B38a7WB3Y05fdmffGB9qRxIimiPiefMvYj2ChHgvrvG4uMlKrlCrktLxvpbbvjlikByy8NbqP/32zH/eqT+VPJk+1ZonebH1T6GSPaHd/0xkt0UDCRXtMes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072xPbywefpqAqY1F9w38fWDLnTv/t8up82lO6nwH4tBtLDJc+B+X/ERfkamxaG7/4zZbX6EdaCw64bpdTufro7t4/thuZZKNRfMDm7pXV1HVbksk1+kzZrXufifTWJijma6ACle0H/RdgW49H4U0rujtW9/nxsVqxEch9ZsQnXWfOzldbq0t179A8LNC68B+buOnbg8kdpq8TfK6/na203fhb13UW3f+b51fXRnZBEzY/shtvs6xt2tJx/B8eq11Pp/Pr7tK7Y9COnvsfMHk/aXL1//2dQzdVyWfGngwF42FOZrpCqhwRfvBh8aicQPheSegvubPM63vZtauzPUVEm+ynyv3XmUfV6Lmlhv3NFKvanw5o3VMPTutb+1nb+9fNOn74KC56+eWa7+e8t5X9+QTe//ZwXX29j99FPIK6HNrw/dF2jGstQfpb9gO3J3qfHvzfr+P+KSjb4W+jqTrno/GwhzNdAVUuKItUN/HGcF1363va/tLmtxY9K3W/SEjjYU5mukKqHBFW6B9Nhapr1fsafuLqgb/TEW3q0h2HlXP5yPuWIRezNFHU0CFK1ogL42FOZrpCqhwRQvkpbEwRzNdARWuaIG8NBbmaKYroMIVLZCXv7zpjxgy3eb1pmiBaMwpAEA2GgsAIBuNBQCQjcYCAMhGYwEAZKOxAACy0VgAANloLACAbDQWAEA2GgsAIBuNBQCQjcYCAMhGYwEAZKOxAACy0VgAANloLACAbDQWAEA2GgsAIBuNBQCQjcYCAMjm/333BkLhXUcvAAAAAElFTkSuQmCC" alt="" />

安装 Owin 程序包

Microsoft.AspNet.Identity.Owin 包含一组 OWIN 扩展类,用于管理和配置 OWIN 身份验证中间件,这些类由 ASP.NET Identity.Core 程序包使用。
Microsoft.Owin.Host.SystemWeb 包含一个 OWIN 服务器,使基于 OWIN 的应用程序可在 IIS 集成管道内运行。

添加 OWIN 启动类和身份验证配置类

[assembly: OwinStartup(typeof(Startup))]

public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// 当用户登录时使应用程序可以验证安全戳。
// 这是一项安全功能,当你更改密码或者向帐户添加外部登录名时,将使用此功能。
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
}); }
}

OwinStartupAttribute 用于配置启动类,默认情况下会调用名为 Configuration 的方法,也可通过 MethodName 指定不同的方法名。

修改用户注册代码

使用户在注册后自动登录

var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore); var user = new IdentityUser() { UserName = ... };
IdentityResult result = manager.Create(user, ...); if (result.Succeeded)
{
var authenticationManager = this.HttpContext.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
}
else
{
}

ASP.NET Identity 和 OWIN Cookie Authentication 是基于声明的,因此需要为用户生成 ClaimsIdentity 对象。ClaimsIdentity 包含用户所有的声明信息,例如,用户属于哪些角色。可以在这里为用户添加更多声明。通过调用 AuthenticationManager.SignIn 方法使用户登录。IAuthenticationManager 接口在 OWIN 中定义。这段代码会产生一个 cookie,类似于 Forms 身份验证的 FormAuthentication.SetAuthCookie 方法。

添加登录代码

var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(...); if (user != null)
{
var authenticationManager = this.HttpContext.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
// 登录成功
}
else
{
// 登录失败
}

GetOwinContext 是一个扩展方法,在 Microsoft.Owin.Host.SystemWeb 程序集中定义,它返回 IOwinContext 对象,OwinContext 公布 IAuthenticationManager 属性,表示对本次请求可用的身份验证中间件。

添加注销代码

var authenticationManager = this.HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut();

原文地址:http://www.asp.net/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project

04-30 18:54