pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

代碼:TokenNotFound 消息:在令牌緩存中找不到用戶

Code: TokenNotFound Message: User not found in token cache. Maybe the server was restarted(代碼:TokenNotFound 消息:在令牌緩存中找不到用戶.可能服務器重啟了)
本文介紹了代碼:TokenNotFound 消息:在令牌緩存中找不到用戶.可能服務器重啟了的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有以下功能從活動目錄調用用戶使用圖形 api.此功能在文本框的每個鍵上都被擊中.但我收到以下錯誤

I have the following function to call users from active directory use graph api. This function is hit on each keyup of a text box. But i am getting following error

代碼:TokenNotFound 消息:在令牌緩存中找不到用戶.也許服務器已重新啟動.

Code: TokenNotFound Message: User not found in token cache. Maybe the server was restarted.

排隊

var user = await graphClient.Users.Request().GetAsync();

整個函數如下:

     public async Task<string> GetUsersJSONAsync(string textValue)
            {
               // email = email ?? User.Identity.Name ?? User.FindFirst("preferred_username").Value;
                var identifier = User.FindFirst(Startup.ObjectIdentifierType)?.Value;
                var graphClient = _graphSdkHelper.GetAuthenticatedClient(identifier);
                string usersJSON = await GraphService.GetAllUserJson(graphClient, HttpContext, textValue);
                return usersJSON;
            }


    public static async Task<string> GetAllUserJson(GraphServiceClient graphClient, HttpContext httpContext, string textValue)
            {
               // if (email == null) return JsonConvert.SerializeObject(new { Message = "Email address cannot be null." }, Formatting.Indented);

                try
                {
                    // Load user profile.
                    var user = await graphClient.Users.Request().GetAsync();                    
                    return JsonConvert.SerializeObject(user.Where(u => !string.IsNullOrEmpty(u.Surname) && ( u.Surname.ToLower().StartsWith(textValue) || u.Surname.ToUpper().StartsWith(textValue.ToUpper()))), Formatting.Indented);
                }
                catch (ServiceException e)
                {
                    switch (e.Error.Code)
                    {
                        case "Request_ResourceNotFound":
                        case "ResourceNotFound":
                        case "ErrorItemNotFound":
                        //case "itemNotFound":
                        //    return JsonConvert.SerializeObject(new { Message = $"User '{email}' was not found." }, Formatting.Indented);
                        //case "ErrorInvalidUser":
                        //    return JsonConvert.SerializeObject(new { Message = $"The requested user '{email}' is invalid." }, Formatting.Indented);
                        case "AuthenticationFailure":
                            return JsonConvert.SerializeObject(new { e.Error.Message }, Formatting.Indented);
                        case "TokenNotFound":
                            await httpContext.ChallengeAsync();
                            return JsonConvert.SerializeObject(new { e.Error.Message }, Formatting.Indented);
                        default:
                            return JsonConvert.SerializeObject(new { Message = "An unknown error has occured." }, Formatting.Indented);
                    }
                }
            }


 // Gets an access token. First tries to get the access token from the token cache.
        // Using password (secret) to authenticate. Production apps should use a certificate.
        public async Task<string> GetUserAccessTokenAsync(string userId)
        {
            _userTokenCache = new SessionTokenCache(userId, _memoryCache).GetCacheInstance();

            var cca = new ConfidentialClientApplication(
                _appId,
                _redirectUri,
                _credential,
                _userTokenCache,
                null);

            if (!cca.Users.Any()) throw new ServiceException(new Error
            {
                Code = "TokenNotFound",
                Message = "User not found in token cache. Maybe the server was restarted."
            });

            try
            {
                var result = await cca.AcquireTokenSilentAsync(_scopes, cca.Users.First());
                return result.AccessToken;
            }

            // Unable to retrieve the access token silently.
            catch (Exception)
            {
                throw new ServiceException(new Error
                {
                    Code = GraphErrorCode.AuthenticationFailure.ToString(),
                    Message = "Caller needs to authenticate. Unable to retrieve the access token silently."
                });
            }
        }

你能幫忙看看出了什么問題嗎?

Can you help whats going wrong?

推薦答案

我知道這已經 4 個月大了 - 這對你來說仍然是個問題嗎?

I know this is 4 months old - is this still an issue for you?

正如之前的受訪者所指出的,您看到的錯誤是在您的代碼中的 catch 塊中拋出的,該代碼用于處理空的 users 集合.

As the previous respondent pointed out, the error you're seeing is being thrown in the catch block in your code meant to handle an empty users collection.

如果你被困在這個問題上,或者其他人來這里 - 如果你使用 this sample (或在任何方面使用 ConfidentialClientApplication )并拋出此異常,這是因為您的 _userTokenCache 沒有用戶*.當然不是因為你的AD沒有用戶,否則你就無法認證.很可能是因為您的瀏覽器中的一個陳舊的 cookie 作為訪問令牌傳遞給您的 authProvider.您可以使用 Fiddler(或僅檢查您的 localhost 瀏覽器 cookie)來找到它(應該稱為 AspNetCore.Cookies,但您可能需要清除所有這些).

In case you're stuck on this, or anyone else comes here - if you used this sample (or using ConfidentialClientApplication in any respect) and are throwing this exception, it's because your _userTokenCache has no users*. Of course, it's not because your AD has no users, otherwise you wouldn't be able to authenticate. Most likely, it is because a stale cookie in your browser is being passed as the access token to your authProvider. You can use Fiddler (or just check your localhost browser cookies) to find it (should be called AspNetCore.Cookies, but you may want to clear all of them).

如果您將令牌緩存存儲在會話中(如示例所示),請記住,每次啟動和停止應用程序時,您的工作內存都會被丟棄,因此您的瀏覽器提供的令牌將不再匹配新的您的應用程序將在重新啟動時檢索到的一個(除非您再次清除了瀏覽器 cookie).

If you're storing the tokencache in session (as the example is), remember that each time you start and stop the application, your working memory will be thrown out so the token provided by your browser will no longer match the new one your application will retrieve upon starting up again (unless, again, you've cleared the browser cookies).

*cca.Users - 您必須使用 cca.GetAccountsAsync().如果您的已部署應用程序使用已棄用的 IUser 實現運行,則必須更改此設置.否則,在開發過程中,您的編譯器會抱怨并且不允許您構建,所以您已經知道了這一點.

*cca.Users is no longer used or supported by MSAL - you have to use cca.GetAccountsAsync(). If you have a deployed application running with the deprecated IUser implementation, you'll have to change this. Otherwise, in development your compiler will complain and not let you build, so you'll already know about this.

這篇關于代碼:TokenNotFound 消息:在令牌緩存中找不到用戶.可能服務器重啟了的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司 | 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 预制舱-电力集装箱预制舱-模块化预制舱生产厂家-腾达电器设备 | 内六角扳手「厂家」-温州市威豪五金工具有限公司 | 储能预警-储能消防系统-电池舱自动灭火装置-四川千页科技股份有限公司官网 | 磁力抛光研磨机_超声波清洗机厂家_去毛刺设备-中锐达数控 | 活动策划,舞台搭建,活动策划公司-首选美湖上海活动策划公司 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 风化石头制砂机_方解石制砂机_瓷砖石子制砂机_华盛铭厂家 | 超高频感应加热设备_高频感应电源厂家_CCD视觉检测设备_振动盘视觉检测设备_深圳雨滴科技-深圳市雨滴科技有限公司 | 设计圈 - 让设计更有价值!| ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! | 福建珂朗雅装饰材料有限公司「官方网站」 | 长城人品牌官网| app开发|app开发公司|小程序开发|物联网开发||北京网站制作|--前潮网络 | 苏州柯瑞德货架-仓库自动化改造解决方案 | T恤衫定做,企业文化衫制作订做,广告T恤POLO衫定制厂家[源头工厂]-【汉诚T恤定制网】 | 早报网| 新密高铝耐火砖,轻质保温砖价格,浇注料厂家直销-郑州荣盛窑炉耐火材料有限公司 | 蓝牙音频分析仪-多功能-四通道-八通道音频分析仪-东莞市奥普新音频技术有限公司 | 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 南京PVC快速门厂家南京快速卷帘门_南京pvc快速门_世界500强企业国内供应商_南京美高门业 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | DAIKIN电磁阀-意大利ATOS电磁阀-上海乾拓贸易有限公司 | 澳威全屋定制官网|极简衣柜十大品牌|衣柜加盟代理|全屋定制招商 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | COD分析仪|氨氮分析仪|总磷分析仪|总氮分析仪-圣湖Greatlake | 隧道风机_DWEX边墙风机_SDS射流风机-绍兴市上虞科瑞风机有限公司 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | CCE素质教育博览会 | CCE素博会 | 教育展 | 美育展 | 科教展 | 素质教育展 | 空冷器|空气冷却器|空水冷却器-无锡赛迪森机械有限公司[官网] | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | 鲁网 - 山东省重点新闻网站,山东第一财经门户|