【问题标题】:Authenticate to Azure API App using ADAL using Token or Not使用 ADAL 使用令牌或不向 Azure API 应用程序进行身份验证
【发布时间】:2015-09-28 15:09:22
【问题描述】:

我们正在使用我们的 TokenHandler 来生成令牌

我们的网址看起来像这样http://abc.go.com?token=1234

现在我们想使用 Azure AD 生成令牌,是否可以在不创建特殊的 http 请求标头的情况下进行服务器端令牌验证,这样我们只使用当前接口作为令牌,但令牌来自 azure ad?

【问题讨论】:

    标签: c# .net azure active-directory


    【解决方案1】:

    您可以针对 AAD 授权进行手动 jwt 令牌验证,并将其放在您拥有的旧 api 路由中。

    你可以有这样的端点:

    public async Task<bool> IsTokenValid(string jwtToken)
    {
        JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
    
        TokenValidationParameters validationParameters = new TokenValidationParameters
        {
            ValidAudience = audience,
            ValidIssuer = issuer,
            IssuerSigningTokens = signingTokens,
            CertificateValidator = X509CertificateValidator.None
        };
    
        // Validate token.
        SecurityToken validatedToken = new JwtSecurityToken();
        ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(jwtToken, validationParameters, out validatedToken);
    
        // Set the ClaimsPrincipal on the current thread.
        Thread.CurrentPrincipal = claimsPrincipal;
    
        // Set the ClaimsPrincipal on HttpContext.Current if the app is running in web hosted environment.
        if (HttpContext.Current != null)
        {
            HttpContext.Current.User = claimsPrincipal;
        }
    
        // If the token is scoped, verify that required permission is set in the scope claim.
        if (ClaimsPrincipal.Current.FindFirst(scopeClaimType) != null && ClaimsPrincipal.Current.FindFirst(scopeClaimType).Value != "user_impersonation")
            return await Task.FromResult(false);
    
        return await Task.FromResult(true);
    }
    

    这是一个示例 repo,您​​可以了解如何进行手动令牌验证:

    https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation

    【讨论】:

    • 我们使用的是 C# 和 .net,jwt token 和 oauth2 endpoitn token 有什么区别吗?
    猜你喜欢
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多