ASP.NET Identity 配合 Azure 存储表






4.60/5 (3投票s)
如何:使用 Azure 存储表进行 ASP.NET Identity
引言
本教程旨在填补有关如何使用 Azure 存储表的 ASP.NET Identity 的空白,这对于许多人来说,比使用默认的 SQL Server LocalDB 和 EntityFramework 来说是一个更好的选择。
背景
Using the Code
可下载的项目使用单页应用程序模板,并且已完成所有这些步骤。在使用它之前,必须配置 AzureStore 的连接字符串。我还删除了所有 NuGet 包,以便文件大小低于 10MB,Visual Studio 应该在构建解决方案时恢复它们。
- 创建一个 ASP.NET(已测试 4.5 或更高版本)Web 应用程序,并使用 MVC、Web API 或单页应用程序模板,保留默认的**Individual User Accounts**身份验证。
- 来自 NuGet
- 安装程序包
WindowsAzure.Storage
- 删除这些程序包
Microsoft.AspNet.Identity.EntityFramework
EntityFramework
- [可选] 在程序包管理器控制台中,运行
Update-Package
以更新所有内容
- 安装程序包
- 在Models文件夹中,创建一个类来匹配您的 Azure 存储表用户数据 - 此处命名为
AzureTableUser
- 该类必须继承自
TableEntity
和IUser
。 - 实现
IUser
接口(右键单击 > 实现接口)- 在我的例子中,ID 变量未使用,所以我让它返回
UserName
- 我将
UserName
变量设为自动实现的属性,因为这在我的情况下已足够
- 在我的例子中,ID 变量未使用,所以我让它返回
- 添加这些属性
public string Password { get; set; }
public string Email { get; set; }
public int FailedLogIns { get; set; }
public DateTimeOffset LockOutEndDate { get; set; }
- 如果您要使用
Roles
,请添加public IList<string> Roles { get; set; }
- 如果您要使用
Claims
,请添加public IList<string> Claims { get; set; }
- 该类必须继承自
- 在helpers文件夹中,创建一个类来处理您的存储表 - 此处命名为
AzureStore
- 如果需要,请为您的模型命名空间添加
using
语句 - 该类必须至少继承自
Microsoft.AspNet.Identity
命名空间中的 4 个接口,它们是IUserStore<
AzureTableUser>
IUserPasswordStore<AzureTableUser>
IUserLockoutStore<AzureTableUser, TKey>
IUserTwoFactorStore<AzureTableUser, TKey>
- 我使用
string
作为Lockout
和TwoFactor
存储的TKey
,因为这样就可以使用string
来实现GetUserById
/GetUserByName
。 - 实现所有接口。我建议使用
#region
指令来分隔实现。 - 添加一个
private readonly
CloudTable cloudTable
字段,它将负责在您的存储表上执行命令。 - 创建一个构造函数,并执行以下操作以获取对表的引用
StorageUri storageUri = new StorageUri(new Uri("https://yourstorageaccountname.table.core.windows.net/"));
CloudStorageAccount csa = CloudStorageAccount.Parse("connectionString");
cloudTable =
csa.CreateClouldTableClient().GetTableReference("yourTableName");
- 如果需要,请为您的模型命名空间添加
- 打开主Web.config文件,并删除
EntityFramework
部分。 - 打开
Startup.Auth
(来自App_Start
)- 删除不必要的
using
语句 - 在
ConfigureAuth
下,删除第一个CreatePerOwinContext
调用,因为我们不会使用模板创建的数据库 - 注释掉在新
CookieAuthenticationOptions
中分配OnValidateIdentity
的行 - 除非在 AzureTableUser 中实现,否则此功能无效
- 删除不必要的
- 从Models文件夹中,删除IdentityModels.cs文件,因为它将不会被使用
- 打开
IdentityConfig
(来自App_Start
)- 删除不必要的
using
语句 - 除非您要实现自定义的电子邮件和/或短信双因素身份验证,否则请删除
EmailService
和SmsService
类- 在
ApplicationUserManager
类的create
函数中,删除对 newEmailService
和 newSmsService
的调用
- 在
- 对于每个对默认
ApplicationUser
类的调用,将其更改为 AzureTableUser - 在
Create
下的 newApplicationuserManager
调用中,将新创建的AzureStore
类的实例作为参数传递给创建此实例的步骤 7。 - 在
ApplicationSignInManager
类中,删除对CreateUserIdentityAsync
的重写
- 删除不必要的
- 打开
AccountController
- 将对默认
ApplicationUser
的调用更改为 AzureTableUser - 删除对上述类的 Hometown 属性的调用,除非您已实现
- 将对默认
- 打开
ManageController
- 将对
user.PasswordHash
的调用更改为user.Password
- 如果您在 AzureTableUser 类中实现了电话号码属性,请将对
user.PhoneNumber
的调用更改为该属性。 - 否则,请删除与电话号码相关的所有视图和方法。
- 将对
- 打开
MeController
- 删除不必要的
using
语句 - 除非您已实现,否则在
Get
方法中删除对user.Hometown
的调用
- 删除不必要的
关注点
在首次执行此操作时,我发现创建AzureStore
类可能相当具有挑战性。即使您不打算锁定账户或使用双因素身份验证,该存储也必须实现这些接口并且不能抛出异常,因此至少必须进行虚拟/硬编码的实现。
历史
- 2015/11/1:第一个版本