0

在数据库中,我有以下表格:USERS、USERS_PROFILES、USERS_CLAIMS。

create table dbo.USERS
(
  Id int identity not null, 
  Username nvarchar (120) not null,
  Email nvarchar (120) not null
);
create table dbo.USERS_PROFILES
(
  Id int not null,
  [Name] nvarchar (80) not null
);
create table dbo.USERS_CLAIMS
(
  Id int not null,
  [Type] nvarchar (200) not null,
  Value nvarchar (200) not null,
);

我正在使用索赔授权。当用户注册并创建身份时。身份包含声明,每个声明都有一个类型和一个值:

UsernameType > 来自 USERS 的用户名 EmailType > 来自 USERS NameType 的电子邮件 > 来自 USERS_PROFILES RoleType 的名称 > 直接来自 USERS_CLAIMS

所以我从 3 个表中的许多列创建身份。

我最终得到了这个,因为我迁移到了声明身份验证。

问题

我应该将用户名、电子邮件和姓名移动到 USERS_CLAIMS 吗?USERS_PROFILES 表将消失……而 USERS 表将仅包含诸如“UserId,LastLoginDate,CreatedDate,……”之类的信息

如果我想通过用户名获取用户,我只会得到用户名类型的声明......

如果我想登录用户,我只需获得所有声明并创建身份。

所以身份模型与 SQL 表非常相似。

这有意义吗?你会如何设计桌子?

谢谢你,米格尔

4

2 回答 2

2

您正在创建一个键值存储。它们是用 SQL 查询的噩梦。考虑通过表上的值查询用户属性的难度USER_CLAIMS。例子:

-- Users with name and email by username
SELECT p.ID, p.Username, p.Name, p.Email, u.LastLoggedIN
FROM USER_PROFILES p
INNER JOIN Users u on p.ID = u.ID
WHERE p.ID = @UserID

-- Users with name and email by username with a claims table
-- Does not specify whether there is only one email, so this could return multiple
-- rows for a single user.
SELECT p.ID, cUName.Value as Username, cName.Value as Name, cEMail.Value as Email, u.LastLoggedIN
FROM Users u
LEFT OUTER JOIN USER_CLAIMS cName ON u.ID = cName.ID and cName.[Type] = 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'
LEFT OUTER JOIN USER_CLAIMS cUName ON u.ID = cUName.ID and cUName.[Type] = 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier'
LEFT OUTER JOIN USER_CLAIMS cEmail ON u.ID = cEmail.ID and cEmail.[Type] = 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/email'
WHERE p.ID = @UserID
于 2013-10-11T00:26:30.173 回答
2

一个用户可以有多个配置文件吗?如果没有,则不需要“USERS_PROFILES”表。保留“USERS”表中的“用户名”和“电子邮件”列。如果您将它们放在“USERS_CLAIMS”表中,那么您将在用户提出索赔时存储冗余信息。

我不确定您希望为您的用户提供什么样的跟踪,但我建议您使用一个单独的表格来跟踪用户何时登录。像这样:

CREATE TABLE USERS_LOG (user_id INT, log_in DATETIME);

然后,您可以摆脱“USERS”表上的“LastLoginDate”并进行连接以获取用户最后一次登录的时间。它将为您提供更多跟踪用户的方法,并且您不会在上创建块通过不断更新您的“用户”表。

于 2013-10-11T00:30:43.857 回答