如何使用 Identity Server 4 颁发基于 Windows 身份验证的访问令牌

2024-04-19

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

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

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

我尝试使用延期补助金(http://docs.identityserver.io/en/release/topics/extension_grants.html http://docs.identityserver.io/en/release/topics/extension_grants.html)并让 ValidateAsync() 方法成为针对 AD 进行身份验证的方法,但无法使其工作(主要是因为 HttpContext 不可用)。这是正确的方法吗?

Update

在这个系统中,客户端是一个控制台应用程序(没有人工交互),因此上下文是运行该应用程序的帐户。 我一直在运行 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 验证客户端上下文来颁发访问令牌。如果在这种情况下必须使用隐式或混合流,那么该怎么做?


我有同样的要求并使用延期补助金来实现它。
这是延期补助金的代码:

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");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Identity Server 4 颁发基于 Windows 身份验证的访问令牌 的相关文章

随机推荐