【问题标题】:IdentityServer3 connect/token endpoint always return 401: unauthorizedIdentityServer3 连接/令牌端点始终返回 401:未授权
【发布时间】:2018-06-24 06:08:36
【问题描述】:

我正在尝试为我的项目设置 IdentityServer3。

当我在本地开发机器上运行 IdentityServer3 时,一切正常,但是当我将它托管在共享服务器上时,我收到 401 错误。我正在尝试使用端点连接令牌访问令牌。这是 identityserver3 的配置

IdentityServerOptions identityServerOptions = new IdentityServerOptions
{
    SiteName = "Ripple IdentityServer",
    SigningCertificate = LoadCertificate(),
    AuthenticationOptions = new IdentityServer3.Core.Configuration.AuthenticationOptions
    {
        EnablePostSignOutAutoRedirect = true,
    },
    LoggingOptions = new LoggingOptions
    {
        EnableWebApiDiagnostics = true,
        WebApiDiagnosticsIsVerbose = true,
        EnableHttpLogging = true,
        EnableKatanaLogging = true
    },

    Factory = factory,
};

奇怪的是我没有得到任何日志。我知道日志正在工作,因为当我访问连接/授权端点时,我可以看到日志信息。这是我的客户注册

client = new Client
{
    ClientId = app.Id,
    ClientName = app.Name,
    Flow = Flows.ResourceOwner,
    AllowedScopes = app.AllowedScopes.Split(';').ToList(),
    AllowedCorsOrigins = new List<string> { "*" }
};
if (app.Secret != null && app.Secret != "")
{
    client.ClientSecrets = new System.Collections.Generic.List<Secret>();
    app.Secret = app.Secret.Replace("{", "").Replace("}", "");

    string[] secrets = app.Secret.Split(',');
    foreach (var s in secrets)
    {
        client.ClientSecrets.Add(new Secret(s.Sha256()));
    }
}

这是获取访问令牌的客户端代码

var data = new StringContent(string.Format("grant_type=password&username={0}&password={1}&Domain={2}&scope={3}",
HttpUtility.UrlEncode(username),
HttpUtility.UrlEncode(password),
HttpUtility.UrlEncode(domainId),
HttpUtility.UrlEncode(requiredScope)), Encoding.UTF8, "application/x-www-form-urlencoded");

client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue(
                "Basic",
                Convert.ToBase64String(
                System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", applicationId, appSecretKey))));

HttpResponseMessage response = client.PostAsync("connect/token", data).Result;

没有日志,我完全迷失了。我应该在哪里查找更多信息以进行调试?

【问题讨论】:

  • 我的客户端应用程序是 Windows 应用程序,我正在尝试从我的桌面执行。
  • 使用像wireshark或fiddler这样的嗅探器。我怀疑用户名和凭据不起作用。可能是因为凭据未加密而被阻止。我会检查您是否可以使用浏览器访问该站点。如果是这样,将浏览器中的嗅探器结果与您的 c# 代码进行比较。比较 http 标头。在浏览器中存在的 c# 中添加缺少的标头。
  • 我使用 fiddler 来捕获流量。即使使用提琴手作曲家,我也会得到相同的结果。我知道凭据是正确的,因为相同的凭据适用于连接/授权端点
  • 所以你在 fiddler 中得到的状态是 401?连接/授权返回一个 cookie。您是否对其他请求使用相同的 cookie?首先删除所有 cookie。第一个请求可能会也可能不会发送 cookie,具体取决于 cookie 是否过期。当您发送没有 cookie 的第一个请求时,需要用户名和密码。响应将发回一个 cookie。然后你必须在下一个请求中使用 cookie。服务器可能不允许来自同一 IP 的多个连接,这就是您收到错误的原因。
  • 是的,即使使用 Fiddler,我也会得到 401。我是

标签: c# asp.net token identityserver3 endpoint


【解决方案1】:

找到解决方案。 Godaddy 等共享主机不支持基本身份验证。因此,访问令牌的请求在服务器级别被拒绝。这就是没有生成日志文件的原因。

要解决这个问题,我必须在 ISecretParser 上实现我自己的版本。在这个实现中,我解析了我自己的身份验证标头

例如身份验证 MyAuth ClientID:ClientSecret

然后用 IdentityServerServiceFactory 注册这个解析器,它就像魅力一样工作。

我希望这个解决方案能帮助其他试图在共享服务器上托管 IdentiyServer3 的人。

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2017-06-11
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多