Web api中Token的目的是确保用户的身份。这是我创建令牌的方式。
public AuthTokenModel GenerateToken(string UserName, string password, string appCode)
{
var tokenExpiration = TimeSpan.FromSeconds(Convert.ToDouble(GetAppSettingsValue("TokenExpiry")));
ClaimsIdentity identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
identity.AddClaim(new Claim("loginId", UserName));
identity.AddClaim(new Claim("password", password));
identity.AddClaim(new Claim("applicationCode", appCode));
var props = new AuthenticationProperties()
{
IssuedUtc = DateTime.UtcNow,
ExpiresUtc = DateTime.UtcNow.Add(tokenExpiration),
};
var ticket = new AuthenticationTicket(identity, props);
var accessToken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);
AuthTokenModel model = new AuthTokenModel();
model.AccessToken = string.Format("{0} {1}", "bearer", accessToken);
model.TokenType = "bearer";
model.ExpiresIn = tokenExpiration.TotalSeconds.ToString();
model.Expires = ticket.Properties.ExpiresUtc.ToString();
model.Issued = ticket.Properties.IssuedUtc.ToString();
return model;
}
,模型定义为:
public class AuthTokenModel
{
public string AccessToken { get; set; }
public string TokenType { get; set; }
public string ExpiresIn { get; set; }
public string Issued { get; set; }
public string Expires { get; set; }
}
但我想知道我是否在Token中添加更多用户的详细信息是更好的方法?如果我从令牌而不是数据库中获得更多细节,那么性能如何。
答案 0 :(得分:0)
我认为这将是理解网络应用程序中基于令牌的身份验证的良好开端。