【问题标题】:Asp.Net Core Api AuthorizationAsp.Net Core Api 授权
【发布时间】:2020-09-25 11:39:36
【问题描述】:

我有一个 Asp.Net Core / ReactJs 应用程序。我正在使用Microsoft.AspNetCore.ApiAuthorization.IdentityServer 来验证 API。但是,我遇到了一个错误,我认为这是由于配置错误。

如果我使用以下配置在本地运行它,一切正常,并且应用程序会按预期重定向到登录屏幕:

  "IdentityServer": {
    "Clients": {
      "MyApp": {
        "Profile": "IdentityServerSPA",        
      }
    },
    "Key": {
      "Type": "File",
      "FilePath": "Assets/selfsignedcert.pfx",
      "Password": "password"
    }
  },

但是,如果我将配置更改为以下内容:

  "IdentityServer": {
    "Clients": {
      "MyApp": {
        "Profile": "IdentityServerSPA",        
        "RedirectUri": "https://localhost:5211/authentication/login-callback"
        "LogoutUri": "https://localhost:5211/authentication/logout-callback"        
      }
    },
    "Key": {
      "Type": "File",
      "FilePath": "Assets/selfsignedcert.pfx",
      "Password": "password"
    }
  },

It 错误(重定向到以下内容):

https://localhost:5211/home/error?errorId=1234...

从客户端看auth请求,它们是完全一样的;但是第二个返回错误,而第一个成功重定向。

我的配置有问题吗?或者,我该如何调试这个问题?

【问题讨论】:

  • 能否请您也分享一下错误
  • 我得到的只是重定向到错误页面。
  • 您是否检查过浏览器的开发者工具(或 Fiddler)以了解重定向是如何发生的?
  • 是的。如果配置“不好”,它会从身份验证请求重定向

标签: reactjs asp.net-core authentication


【解决方案1】:

非工作配置中的两个 Uris 都指向注销。这真的是你想要做的吗?

在第一个配置中,您没有定义任何 Uris,因此它们将具有以下默认值:

redirect_uri 默认为 /authentication/login-callback。 post_logout_redirect_uri 默认为 /authentication/logout-callback。

据记录 here

尝试更改第二个配置以匹配默认值,看看是否有帮助。

【讨论】:

  • 道歉:我在整理副本时显然犯了某种错误,事实上,RedirectUri 确实指向登录回调(我已经在问题中更新了它)。您是正确的, RedirectUri 默认指向 /authentication/login-callback ;如果我明确地将其更改为默认值,那么它就可以工作。但是,我无法使用默认值部署它 - 并指定完整的 url(如您链接到的示例中所述)重定向到错误页面
【解决方案2】:

redirect_uripost_logout_redirect_uri的客户端配置必须和IDP一致:

redirect_uri: 'https://localhost:5211/authentication/login-callback',
post_logout_redirect_uri: 'https://localhost:5211/authentication/logout-callback',

或者

redirect_uri: $'{IDPhost_config}/authentication/login-callback',
post_logout_redirect_uri: $'{IDPhost_config}/authentication/logout-callback',

【讨论】:

  • 这不是我目前拥有的(如果我遗漏了一些明显的东西,请见谅)?
  • 你的问题是缺少客户端的配置吧?
  • 文档似乎暗示客户端配置是从客户端配置文件翻译而来的:docs.microsoft.com/en-us/aspnet/core/security/authentication/…
  • 你认为我在哪里缺少这个配置?
猜你喜欢
  • 2023-03-12
  • 2018-09-29
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 2018-04-03
相关资源
最近更新 更多