我使用this教程设置了identityserver3和MVC4客户端。当我将客户端配置为使用“隐式”流时,事情正在按预期工作,我正在回到“配置文件”范围。即我可以找到声明first_name和given_name。在我的配置代码下面。
客户端和用户配置
public static class Users
{
public static List<InMemoryUser> Get()
{
return new List<InMemoryUser>
{
new InMemoryUser
{
Username = "Bob",Password = "password",Subject = "1",
Claims = new []
{
new Claim(Constants.ClaimTypes.GivenName,"firstName"),
new Claim(Constants.ClaimTypes.FamilyName,"lastName")
}
}
};
}
}
public static class Clients
{
public static IEnumerable<Client> Get()
{
return new[]
{
new Client
{
ClientId = "MVC",
ClientName = "MVC Client Name",
RedirectUris = new List<string>
{
"https://localhost:44302/"
},
Flow = Flows.Implicit,
AllowAccessToAllScopes = true
}
};
}
}
身份服务器配置
public void Configuration(IAppBuilder app)
{
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
app.Map("/identity", appBuilder => {
appBuilder.UseIdentityServer(new IdentityServer3.Core.Configuration.IdentityServerOptions
{
SiteName = "Site Name",
SigningCertificate = LoadCertificate(),
RequireSsl = false,
Factory = new IdentityServer3.Core.Configuration.IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryUsers(Users.Get())
.UseInMemoryScopes(StandardScopes.All)
});
});
app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://localhost:44302/identity",
ClientId = "MVC",
RedirectUri = "https://localhost:44302/",
ResponseType = "id_token",
SignInAsAuthenticationType = "Cookies",
Scope = "openid profile"
});
}
在我的MVC应用程序中,我已在家庭控制器上获得名为“联系人”的行动
[Authorize]
public ActionResult Contact()
{
ClaimsPrincipal principal = User as ClaimsPrincipal;
return View(principal.Claims);
}
最后这里是简单的视图
@model IEnumerable<System.Security.Claims.Claim>
@foreach (var item in Model)
{
<div>
<span>@item.Type</span>
<span>@item.Value</span>
</div>
}
</div>
现在,当我运行此应用程序时,点击安全的“联系”链接后,我被重定向到STS服务器,在提供凭据后,我可以在下面看到输出。
请注意,STS返回的声明中存在声明 given_name 和 family_name 。
问题:
我改变客户端以支持混合流的那一刻。我没有支持 given_name 和 family_name
我在下面修改了我的代码。
客户端配置
public static IEnumerable<Client> Get()
{
return new[]
{
new Client
{
ClientId = "MVC",
ClientName = "MVC Client Name",
RedirectUris = new List<string>
{
"https://localhost:44302/"
},
Flow = Flows.Hybrid,//Changed this to Hybrid
AllowAccessToAllScopes = true
}
};
}
服务器配置
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://localhost:44302/identity",
ClientId = "MVC",
RedirectUri = "https://localhost:44302/",
ResponseType = "code id_token token", //Changed response type
SignInAsAuthenticationType = "Cookies",
Scope = "openid profile"
});
运行应用程序后,我可以看到STS返回的以下声明
请注意,这次声明 given_name 和 family_name 的声明将会丢失。
我错过了什么吗?
答案 0 :(得分:4)
当您只询问id_token时,用户的所有声明都在id_token中。当您更改获取令牌的请求时(通过询问代码或令牌),只有用户声明配置为&#34; AlwaysInclude&#34;包含在id_token中。必须使用您收到的access_token从用户信息端点检索其余部分。您可以使用IdentityModel库中的帮助程序API轻松访问用户信息端点。我们的示例显示了如何执行此操作:https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/Clients/MVC%20OWIN%20Client%20(Hybrid)/Startup.cs#L66