如何解决Identity Server 4 和 ASP.NET Web Forms Client - 无效的授权类型
我在本地设置了 Identity Server 4 并添加了一个 MVC Net Core 客户端,没有任何问题。
我无法让我的 .Net Framework Web Forms 应用程序运行。
当我尝试点击关于(安全页面).aspx 页面时,出现以下错误:
“抱歉,出现错误:未授权的客户端
客户端的授权类型无效"
我尝试了所有不同的 Granttypes 都没有成功。
我觉得我在 ID4 中的客户端设置不正确。各种博客文章都说我应该使用代码授权,但其他人说使用 id_token。
我已在 ID4 服务器应用程序中按如下方式设置客户端:
new Client
{
ClientId = "aspx",ClientSecrets = { new Secret("secret".Sha256()) },AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,//My web forms aspx client
RedirectUris = { "http://localhost:5969/" },//My web forms aspx client
PostLogoutRedirectUris = { "http://localhost:5969/" },AllowOfflineAccess = true,AllowAccessTokensViaBrowser = true,RequirePkce = false,AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,IdentityServerConstants.StandardScopes.Profile
},}
我的 Web 窗体应用程序中的 Startup.cs(我正在使用来自 https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/Clients/WebFormsClient/Startup.cs 的 \WebFormsClient\ 示例)
我对 https://localhost:5001/connect/userinfo 应该是什么有点困惑 - 我得到 401。
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = "Cookies",ExpireTimeSpan = TimeSpan.FromMinutes(10),SlidingExpiration = true
});
JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "oidc",SignInAsAuthenticationType = "Cookies",Authority = "https://localhost:5001/",ClientId = "aspx",RedirectUri = "http://localhost:5969/",PostLogoutRedirectUri = "http://localhost:5969/",ResponseType = "id_token token",Scope = "openid profile email",UseTokenLifetime = false,Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = async n =>
{
var claims_to_exclude = new[]
{
"aud","iss","nbf","exp","nonce","iat","at_hash"
};
var claims_to_keep =
n.AuthenticationTicket.Identity.Claims
.Where(x => false == claims_to_exclude.Contains(x.Type)).ToList();
claims_to_keep.Add(new Claim("id_token",n.ProtocolMessage.IdToken));
if (n.ProtocolMessage.AccessToken != null)
{
claims_to_keep.Add(new Claim("access_token",n.ProtocolMessage.AccessToken));
var userInfoClient = new UserInfoClient(new Uri("https://localhost:5001/connect/userinfo"),n.ProtocolMessage.AccessToken);
var userInfoResponse = await userInfoClient.GetAsync();
var userInfoClaims = userInfoResponse.Claims
.Where(x => x.Item1 != "sub") // filter sub since we're already getting it from id_token
.Select(x => new Claim(x.Item1,x.Item2));
claims_to_keep.AddRange(userInfoClaims);
}
var ci = new ClaimsIdentity(
n.AuthenticationTicket.Identity.AuthenticationType,"name","role");
ci.AddClaims(claims_to_keep);
n.AuthenticationTicket = new Microsoft.Owin.Security.AuthenticationTicket(
ci,n.AuthenticationTicket.Properties
);
},RedirectToIdentityProvider = n =>
{
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
{
var id_token = n.OwinContext.Authentication.User.FindFirst("id_token")?.Value;
n.ProtocolMessage.IdTokenHint = id_token;
}
return Task.FromResult(0);
}
}
});
app.UseStageMarker(PipelineStage.Authenticate);
}
}
解决方法
“id_token 令牌”是 Implicit flow,因此您需要将其包含在您的 AllowedGrantTypes
中。
对于 UserInfo endpoint,它仅根据您调用它的访问令牌返回有关用户的声明。获得 401 响应可能意味着您没有传递有效的访问令牌。如果您需要额外的用户声明并且您只有访问令牌而没有 ID 令牌,它会很有用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。