新文章 网摘 文章 随笔 日记

IdentityServer3中的一些问题

  • 如果启动IdentityServer时遇到的欢迎页面没有任何有效的CSS,JS或图像,需要在web.config中配置:
<configuration>
  ...
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>

 

  • 若出现Invalid redirect_uri的问题:

在服务端的Clients中加入

RedirectUris = new List<string>()
{
    "http://localhost:49795/"
}
  • Invalid scope

服务端scope与客户端的scope要一致,注意是区分大小写的。

  • 没有启用本地登录

如果服务端的client没有启用本地登录(EnableLocalLogin),则无法跳转到登录页面,这个需要开启。

  • 服务端的Client要配置身份令牌(IdentityTokenLifetime)和访问令牌(AccessTokenLifetime)和授权码有效期(AuthorizationCodeLifetime),否则可能无法访问
  • 如果报错:A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was not present on the provided ClaimsIdentity,则在服务端和客户端的Global.asax的 Application_Start()中加入
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
  •  AuthorizationCodeReceived事件没有触发

需要将客户端的ResponseType 设置为 OpenIdConnectResponseTypes.CodeIdToken并将服务端的Client的Flow设置为Hybrid:

ResponseType = OpenIdConnectResponseTypes.CodeIdToken

但这是这样又无法获得access_Token,所以最终改为

ResponseType = "code id_token token"//可以获得access_token

 

  • 使用TokenClient.RequestAuthorizationCodeAsync请求AccessToken时,返回invalid_client,经查原因竟然是因为服务端配置的ClientSecrets中的Type与客户端的Type不同,客户端默认Type是SharedSecret,将他们设置一致即可。

服务端配置的ClientSecrets必须是经Sha256或Sha512加密后的字符。而客户端则使用未加密前的字符。

 

  • A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was not present on the provided ClaimsIdentity

在Global.asax中加入:

protected void Application_Start() {
//...other code omitted for brevity
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}

 

然后在客户端的AuthorizationCodeReceived中加入:

var userId = notification.AuthenticationTicket.Identity.Claims
.FirstOrDefault(r =>r.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

if (userId != null)
{
    //防止报not present on the provided ClaimsIdentity.的错误
    id.AddClaim(new Claim(ClaimTypes.Name, userId.Value));
}

 

  • 注销:
[AllowAllSignInUser]
public ActionResult LogOff()
{
    Request.GetOwinContext().Authentication.SignOut();
    return RedirectToAction("Index", "Home");
}

 

然后在服务端配置注销后跳转页面(ClientPostLogoutRedirectUris)

  • 在服务端对客户端的配置中去掉需要授权,则两个域名间跳转链接才不会出现需要再次登录的界面(即SSO,一次登录,所有通行),同时需要关闭服务端包括View和Controller的ValidateAntiForgeryToken。(但这样会否产生风险?)
  • 单点注销(Single Sign Out)

1、在服务端中的客户端配置输入客户端注销回调网址(LogoutUri),例如:http://localhost:61953/Account/SignOutCleanup

2、在客户端的Startup.cs中加入:

using IdentityServer3.Core.Extensions;

 

app.Map("/Account/SignOutCleanup", cleanup =>
{
    cleanup.Run(async ctx =>
    {
        await ctx.Environment.ProcessFederatedSignoutAsync();
     });
});

 

注意服务端配置要与客户端配置的回调网址一致。

 

  • MagicOnion发生Status(StatusCode=Unimplemented, Detail="")错误

原因:Service的实现类没有和MagicOnion.Server在同一项目中而是在一个独立的类库中,服务端找不到实现类所以产生这样的错误。
解决方案:

MagicOnionServiceDefinition service = MagicOnionEngine.BuildServerServiceDefinition(
    new[] {typeof(Test).Assembly }, // 手动添加实现类
    new MagicOnionOptions(true)
    {
        MagicOnionLogger = new MagicOnionLogToGrpcLogger()
    }  );    

 


来源:https://github.com/Cysharp/MagicOnion/issues/117

 

  • 错误:IDX10614: AsymmetricSecurityKey.GetSignatureFormater

原因:帐户idsrv运行所在的帐户没有对签名证书的私钥的读取权限。
解决方案:在IIS中,选择问题网站,“基本设置”,“连线身份”选择“应用程式使用者(通过验证)”

posted @ 2020-06-12 15:12  岭南春  阅读(163)  评论(0编辑  收藏  举报