Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用openidconnect从web中的azure获取accesstoken_C#_Azure_Openid Connect - Fatal编程技术网

C# 使用openidconnect从web中的azure获取accesstoken

C# 使用openidconnect从web中的azure获取accesstoken,c#,azure,openid-connect,C#,Azure,Openid Connect,我有一个桌面应用程序,我通过AuthenticationContext.AcquireTokenAsync通过Azure对用户进行身份验证 使用此方法的结果,我可以获取访问令牌,将其发送到我的WCF,并在我的WCF中使用JwtSecurityToken/ConfigurationManager验证令牌 我现在已经在web应用程序中通过Azure实现了登录,方法是使用app.UseOpenIdConnectAuthentication对其进行配置。因此,在我的web应用程序中,我不会显式调用返回令

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

使用此方法的结果,我可以获取访问令牌,将其发送到我的WCF,并在我的WCF中使用
JwtSecurityToken
/
ConfigurationManager
验证令牌

我现在已经在web应用程序中通过Azure实现了登录,方法是使用
app.UseOpenIdConnectAuthentication
对其进行配置。因此,在我的web应用程序中,我不会显式调用返回令牌的方法。相反,我在asp.net的流中加入了这个


但是现在我想在一个方法中获取令牌并发送它进行验证,就像我在我的桌面应用程序中所做的那样。但是,我找不到
ConfigurationManager
接受的任何令牌。我已经查看了常规的
HttpContext
Owincontext
,但没有发现有用的信息。
accesstoken
是否存储在我可以获取它的任何地方?或者我必须执行另一个请求才能获取访问令牌吗?

您应该在响应中获取访问令牌

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

HttpContext.Current.Request.Headers["Authorization"];
另外,我不知道你所说的发送令牌进行验证是什么意思

如果您试图手动验证令牌,下面是一个示例,它正是这样做的-

在示例中,请特别查看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);