【问题标题】:Get accesstoken from azure in web with openidconnect使用 openidconnect 从 web 中的 azure 获取 accesstoken
【发布时间】:2019-02-07 06:30:12
【问题描述】:

我有一个桌面应用程序,我通过 AuthenticationContext.AcquireTokenAsync 通过 Azure 对用户进行身份验证。

使用此方法的结果,我可以获得访问令牌,将其发送到我的 WCF,然后在我的 WCF 中使用 JwtSecurityToken / ConfigurationManager< OpenIdConnectConfiguration > 来验证令牌。

我现在通过使用 app.UseOpenIdConnectAuthentication 配置 Web 应用程序来实现通过 Azure 登录。所以在我的网络应用程序中,我没有明确调用返回令牌的方法。而是我将其插入 asp.net 的流程中。

但是现在我想在一个方法中获取令牌并将其发送以进行验证,就像我在桌面应用程序中所做的那样。但是,我找不到ConfigurationManager 接受的任何令牌。我查看了常规的HttpContextOwincontext,但没有发现有用的信息。 accesstoken 是否存储在我可以获取的任何地方?还是我必须再次请求才能获得accesstoken

【问题讨论】:

    标签: c# azure openid-connect


    【解决方案1】:

    您应该获得访问令牌作为响应的一部分。

    一种简单的方法是查看 Authorization 标头。看看下面的代码 -

    HttpContext.Current.Request.Headers["Authorization"];
    

    另外,我不知道你所说的发送令牌进行验证是什么意思。

    如果您尝试手动验证令牌,这里有一个完全可以做到这一点的示例 -

    Manually validating a JWT access token in a web API

    在示例中,具体看Global.asax.cs

    string jwtToken = null;
    AuthenticationHeaderValue authHeader = request.Headers.Authorization;
    if (authHeader != null)
    {
        jwtToken = authHeader.Parameter;
    }
    if (jwtToken == null)
    {
        HttpResponseMessage response = this.BuildResponseErrorMessage(HttpStatusCode.Unauthorized);
            return response;
    }
    
        .........
        .........
        .........
    
    JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
    
    TokenValidationParameters validationParameters = new TokenValidationParameters
    {
        // We accept both the App Id URI and the AppId of this service application
            ValidAudiences = new[] { audience, clientId },
    
        // Supports both the Azure AD V1 and V2 endpoint
            ValidIssuers = new[] { issuer, $"{issuer}/v2.0" },
            IssuerSigningKeys = signingKeys
    };
    
    try
    {
        // Validate token.
            SecurityToken validatedToken = new JwtSecurityToken();
            ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(jwtToken, validationParameters, out validatedToken);
    

    【讨论】:

      猜你喜欢
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2019-12-24
      • 2013-11-08
      • 1970-01-01
      • 2018-09-30
      相关资源
      最近更新 更多