我们可以为外部登录添加授权范围并将结果保存到 ServiceStack 中的数据库吗? [英] Can we add authorization scopes for external logins and save results to database in ServiceStack?
问题描述
我们能否在 GoogleAuthProvider 中自定义范围以获取更多详细信息,例如他们的电话号码、地址或日历、个人资料图片?
Can we customize the scope in GoogleAuthProvider to get more details like their phone number, address or calendar, profile picture?
我们还可以查看身份和访问令牌的详细信息,并将这些结果解析并保存在我们的数据库中吗?
Also can we view the details of the Identity and access token and parse and save those results in our database?
推荐答案
您可以在 GoogleAuthProvider.Scopes
集合中注册其他范围,其中 默认填充:
You can register additional Scopes in the GoogleAuthProvider.Scopes
collection which by default is populated with:
this.Scopes = new[] {
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email"
};
来自所有 ServiceStack 的 OAuth 提供者的 OAuth 信息是
The OAuth Info from all ServiceStack's OAuth Providers are populated in the registered Auth Repository in the UserAuthDetails table where the Access Token is stored in AccessTokenSecret
.
您可以使用访问令牌并覆盖自定义 GoogleAuthProvider
中的 CreateAuthInfo
并覆盖 CreateAuthInfo()
实现来检索有关用户的其他信息默认情况下,它从 UserProfileUrl
(https://www.googleapis.com/oauth2/v2/userinfo):
You can retrieve additional info about the user using the Access Token and overriding CreateAuthInfo
in a custom GoogleAuthProvider
and overriding the CreateAuthInfo()
implementation which by default retrieves basic info about the user from the UserProfileUrl
(https://www.googleapis.com/oauth2/v2/userinfo):
protected override Dictionary<string, string> CreateAuthInfo(string accessToken)
{
var url = this.UserProfileUrl.AddQueryParam("access_token", accessToken);
var json = url.GetJsonFromUrl();
var obj = JsonObject.Parse(json);
obj.MoveKey("id", "user_id");
obj.MoveKey("given_name", "first_name");
obj.MoveKey("family_name", "last_name");
obj.MoveKey("picture", AuthMetadataProvider.ProfileUrlKey, profileUrl => profileUrl.SanitizeOAuthUrl());
return obj;
}
返回的字典在可覆盖的 LoadUserAuthInfo()(也可以使用每个 AuthProvider 上的 LoadUserAuthFilter
拦截).字典中所有其他不匹配的属性都保存在 UserAuthDetails
表上的 Items
字典中.
The returned dictionary populates all well-known properties on UserAuthDetails
in the overridable LoadUserAuthInfo() (which can alternatively be intercepted with the LoadUserAuthFilter
on each AuthProvider). All other non-matching properties in the dictionary are saved in the Items
Dictionary on the UserAuthDetails
table.
这篇关于我们可以为外部登录添加授权范围并将结果保存到 ServiceStack 中的数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!