向空项目添加 ASP.NET Identity

安装 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 中定义,表示操作结果。

类间关系如图:

安装 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(30),
                    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

原文地址:https://www.cnblogs.com/dongbeifeng/p/adding-aspnet-identity-to-an-empty-project.html