Identityserver4 如何使用Identity Server 4基于Windows身份验证颁发访问令牌
我的目标是保护Web API,使其只能由客户端使用基于Windows身份验证的is颁发的访问令牌进行访问。我完成了这个基本示例: 现在,我需要扩展基本示例,以便返回给客户端的访问令牌是基于Windows身份验证颁发的。更具体地说,当请求访问令牌时,我需要让用户(正在执行客户端应用程序)根据Active Directory进行身份验证。如何做到这一点 我已经成功运行了quick start(),其中登录基于Windows外部提供程序,但我不知道如何将此功能应用到我的策略中 我尝试使用扩展授权()并使用ValidateAsync()方法对AD进行身份验证,但无法使其工作(主要是因为HttpContext不可用)。这是正确的方法吗 更新 在这个系统中,客户端是一个控制台应用程序(没有人工交互),因此上下文是运行应用程序的帐户。 我一直在运行QuickstartUI,了解AccountController逻辑如何处理“Windows”按钮,但无法掌握如何将其与请求访问令牌相结合。我的客户端代码如下所示:Identityserver4 如何使用Identity Server 4基于Windows身份验证颁发访问令牌,identityserver4,Identityserver4,我的目标是保护Web API,使其只能由客户端使用基于Windows身份验证的is颁发的访问令牌进行访问。我完成了这个基本示例: 现在,我需要扩展基本示例,以便返回给客户端的访问令牌是基于Windows身份验证颁发的。更具体地说,当请求访问令牌时,我需要让用户(正在执行客户端应用程序)根据Active Directory进行身份验证。如何做到这一点 我已经成功运行了quick start(),其中登录基于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来获取访问令牌。我不希望存储和使用密码,但基于对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");
客户端凭据不是您的流程。要让用户参与,请使用隐式、授权代码或混合代码。QuickstartUI(继续阅读本教程)已经包括windows身份验证。还值得指出的是,您可以将ADFS 2016配置为与OIDC一起使用,因此,结合隐式/authcode/hybrid flow,您可以将身份验证职责移交给可以简化您的实现的功能—如果您已经有了ADFS,也就是说。感谢您的评论,我已经更新了问题。我希望这能澄清我试图实现的目标。WinAuthConstants从何而来?