我正在使用 Thinktecture AuthenticationConfiguration 为我的 API 上的令牌签名提供一个端点:
var authConfig = new AuthenticationConfiguration
{
EnableSessionToken = true,
SendWwwAuthenticateResponseHeaders = true,
RequireSsl = false,
ClaimsAuthenticationManager = new ClaimsTransformation(),
SessionToken = new SessionTokenConfiguration
{
EndpointAddress = "/api/token",
SigningKey = signingKey,
DefaultTokenLifetime = new TimeSpan(1, 0, 0)
}
};
var userCredentialsService = new CredentialsService(credentialStore);
authConfig.AddBasicAuthentication(userCredentialsService.Validate);
并使用 CredentialsService 对用户进行身份验证:
public class CredentialsService
{
public bool Validate(string username, string password)
{
return username == password;
}
}
上面的工作,当然不是在生产中使用,但是在返回 true 时,我会得到一个令牌,其中包含一个带有用户名的声明。
在我的场景中,我有一个永远不会改变的用户 ID(一个整数),我希望这在我的声明中。因此,用户将在标头中将电子邮件地址传递给服务端点作为基本身份验证,然后如果有效,则继续使用 id 作为声明(但不是作为声明的电子邮件地址)进行签名:
public class CredentialsService
{
public bool Validate(string emailAddress, string password)
{
// map from the provided name, to the user id
var details = MySqlDb.ReadBy(emailAddress);
var id = details.Id; // this is the actual identity of the user
var email = details.EmailAddress;
var hash = details.Hash;
return PasswordHash.ValidatePassword(password,hash);
}
}
我很感激这需要对 sql server 数据库进行第二次查找以将 emailAddress 转换为 userId,有没有办法在调用 CredentialsService 之前将其插入到管道流中?
或者我是不是走错了路,只是坚持使用登录的用户名,然后使用基于用户名的声明转换来丰富整数身份 - 但是如果他们更改了用户名怎么办?