【问题标题】:How to issue access token based on Windows Authentication with Identity Server 4如何使用 Identity Server 4 基于 Windows 身份验证颁发访问令牌
【发布时间】:2018-07-19 21:45:44
【问题描述】:

我的目标是保护 Web API,以便客户端只能使用 IS 基于 Windows 身份验证颁发的访问令牌来访问它。我完成了这个基本示例: http://docs.identityserver.io/en/release/quickstarts/1_client_credentials.html

现在,我需要扩展基本示例,以便返回给客户端的访问令牌是基于 Windows 身份验证发出的。更具体地说,我需要让用户(正在执行客户端应用程序)在请求访问令牌时针对 Active Directory 进行身份验证。这应该怎么做?

我已经成功运行了快速启动 (https://github.com/IdentityServer/IdentityServer4.Templates),其中登录基于 Windows 外部提供程序,但我不知道如何在我的策略中采用此功能。

我尝试使用扩展授权 (http://docs.identityserver.io/en/release/topics/extension_grants.html) 并使用 ValidateAsync() 方法对 AD 进行身份验证,但无法使其工作(主要是因为 HttpContext 不可用)。这甚至是正确的方法吗?

更新

在这个系统中,客户端是一个控制台应用程序(没有人工交互),因此上下文是运行应用程序的帐户。 我一直在运行 QuickstartUI 并查看 AccountController 逻辑如何处理“Windows”按钮,但无法掌握如何将其与请求访问令牌结合起来。我的客户端代码是这样的:

static async Task Main(string[] args)
{
  var disco = await DiscoveryClient.GetAsync("http://localhost:50010");

  var tokenClient = new TokenClient(disco.TokenEndpoint);
  var tokenResponse = await tokenClient.RequestCustomGrantAsync("CustomWindows"); // Not sure about this

  var client = new HttpClient();
  client.SetBearerToken(tokenResponse.AccessToken);

  var response = await client.GetAsync("http://localhost:50011/api/identity");
  var content = await response.Content.ReadAsStringAsync();
  Console.WriteLine(JArray.Parse(content));

  Console.ReadLine();
}

我不确定在这种情况下如何使用 TokenClient 来获取访问令牌。我不希望存储和使用密码,而是让 IS 根据针对 AD 的客户端上下文进行身份验证来发出访问令牌。如果在这种情况下必须使用隐式或混合流,该怎么做?

【问题讨论】:

  • 客户端凭据不适合您。要让用户参与,请使用隐式、授权代码或混合。 QuickstartUI(继续阅读本教程)已经包含 Windows 身份验证。
  • 还值得指出的是,您也可以将 ADFS 2016 配置为与 OIDC 一起使用,因此结合隐式/身份验证码/混合流,您可以将身份验证职责移交给可以简化实施的任务 - 如果您已经有了 ADFS。
  • 感谢您的评论,我已经更新了问题。我希望这能澄清我想要达到的目标。

标签: c# identityserver4


【解决方案1】:

我有相同的要求,并使用扩展授权实现了它。
这是扩展授权的代码:

public class WinAuthGrantValidator : IExtensionGrantValidator
{
    private readonly HttpContext httpContext;

    public string GrantType => WinAuthConstants.GrantType;

    public WinAuthGrantValidator(IHttpContextAccessor httpContextAccessor)
    {
        httpContext = httpContextAccessor.HttpContext;
    }

    public async Task ValidateAsync(ExtensionGrantValidationContext context)
    {
        // see if windows auth has already been requested and succeeded
        var result = await httpContext.AuthenticateAsync(WinAuthConstants.WindowsAuthenticationSchemeName);
        if (result?.Principal is WindowsPrincipal wp)
        {
            context.Result = new GrantValidationResult(wp.Identity.Name, GrantType, wp.Claims);
        }
        else
        {
            // trigger windows auth
            await httpContext.ChallengeAsync(WinAuthConstants.WindowsAuthenticationSchemeName);
            context.Result = new GrantValidationResult { IsError = false, Error = null, Subject = null };
        }
    }
}

这是客户端代码:

var httpHandler = new HttpClientHandler
{
    UseDefaultCredentials = true,
};

// request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret", httpHandler, AuthenticationStyle.PostValues);
var tokenResponse = await tokenClient.RequestCustomGrantAsync("windows_auth", "api1");

【讨论】:

  • WinAuthConstants 是从哪里来的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 2015-10-07
  • 2014-12-17
  • 2019-03-26
  • 2017-06-26
  • 1970-01-01
  • 2019-08-17
相关资源
最近更新 更多