【发布时间】:2015-03-24 21:03:39
【问题描述】:
我正在尝试遵循简单的指南mvcGettingStarted。
现在,我已经实现了GoogleAuthentication 和FacebookAuthentication 提供程序,并且一切都按预期工作,我实际上可以登录,如果我使用我的身份服务器登录,我还可以获得每个用户的角色声明。
我想知道,如果我想保留外部提供商提供的所有索赔怎么办?
简单的例子。
这就是我的 Facebook 提供商设置的样子:
var facebookOptions = new FacebookAuthenticationOptions() {
AuthenticationType = "Facebook",
Caption = "Sign in with Facebook",
AppId = "*****",
AppSecret = "****",
SignInAsAuthenticationType = signInAsType,
Provider = new FacebookAuthenticationProvider() {
OnAuthenticated = (context) => {
foreach (var x in context.User) {
context.Identity.AddClaim(new Claim(x.Key, x.Value.ToString()));
}
return Task.FromResult(context);
}
},
};
facebookOptions.Scope.Add("email");
facebookOptions.Scope.Add("public_profile");
facebookOptions.Scope.Add("user_friends");
app.UseFacebookAuthentication(facebookOptions);
在 for each 循环中,我尝试将所有 Facebook 声明存储在 Identity 中,但是当我返回 SecurityTokenValidated 回调时,我的 Identity 没有它们。
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions() {
Authority = "https://localhost:44302/identity/",
ClientId = "my_client",
Scope = "openid profile roles email",
RedirectUri = "https://localhost:44302/",
ResponseType = "id_token token",
SignInAsAuthenticationType = "Cookies",
UseTokenLifetime = false,
Notifications = new OpenIdConnectAuthenticationNotifications() {
SecurityTokenValidated = async context => {
//let's clean up this identity
//context.AuthenticationTicket.Identity doesn't have the claims added in the facebook callback
var nid = new ClaimsIdentity(
context.AuthenticationTicket.Identity.AuthenticationType,
Constants.ClaimTypes.GivenName,
Constants.ClaimTypes.Role);
........
是因为我在操纵两个不同的身份吗? 有没有正确的方法来实现我想要做的事情? 谢谢你。
【问题讨论】:
标签: c# claims-based-identity thinktecture-ident-server