【问题标题】:UserAssertion missing assertion and dont know where to get itUserAssertion 缺少断言并且不知道从哪里得到它
【发布时间】:2020-09-24 01:18:27
【问题描述】:

所以我有以下代码,我试图用它来代表用户获取访问令牌,以从图形 api 收集信息。

            config.Log.WriteLine(LogLevel.Full, "Gathering OAuth Tokens for " + config.Username + " at " + config.Authority);
            try
            {
                AuthenticationContext ac = new AuthenticationContext(config.Authority);

                ClientCredential cc = new ClientCredential(config.AppKey, config.AppSecret);

                ClientCredential clientCred = new ClientCredential(config.AppKey, config.AppSecret);
                UserAssertion ua = new UserAssertion(//WHAT IS THIS & HOW DO I GET IT,
                                                    "urn:ietf:params:oauth:grant-type:jwt-bearer",
                                                    config.Username);

                Task<AuthenticationResult> re = ac.AcquireTokenAsync("https://graph.windows.net", cc.ClientId, ua);
                while(!re.IsCompleted)
                {
                    re.Wait();
                }
                oauthTokenCache = new OauthTokenCache(re.Result);            
                request.Headers.Add("Authorization", oauthTokenCache.authHeader);
            }
            catch (Exception e)
            {
                Console.WriteLine("Aquire Access Token Exception: " + e);
                throw;
            }

但我不知道UserAssertion 属性“assertion”的来源是什么?断言类型也是一个谜,我找不到 ok 类型的列表,但是在我在网上看到的所有示例中,它们似乎都使用了我输入的那个,但并没有真正帮助我获得断言本身。 有什么帮助吗?谢谢

【问题讨论】:

    标签: c# .net oauth azure-active-directory


    【解决方案1】:

    这是一个 jwt 不记名令牌,您将从您的 HttpContext 获得

    var httpContext = _httpContextAccessor.HttpContext;
    
    //Get the access token used to call this API
    string token = await httpContext.GetTokenAsync("access_token");
    //We are passing an *assertion* to Azure AD about the current user
    //Here we specify that assertion's type, that is a JWT Bearer token
    string assertionType = "urn:ietf:params:oauth:grant-type:jwt-bearer";
    
    var userAssertion = new UserAssertion(token, assertionType, userName);
    
    
    

    查看本指南了解完整流程:https://joonasw.net/view/azure-ad-on-behalf-of-aspnet-core

    当然,我假设您使用的是 MVC。也可以从授权上下文https://docs.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.clients.activedirectory.authenticationcontext?view=azure-dotnet获取它

    【讨论】:

    • 您好,感谢您的回复,不要以为您可以解释如何从授权上下文中获取它,因为我已经在使用一个,而不是使用 HttpContext ,所以这似乎是明智的选择,但我找不到任何可以让我得到这个令牌的方法?
    猜你喜欢
    • 2021-11-07
    • 2020-08-17
    • 1970-01-01
    • 2012-03-23
    • 2012-07-04
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多