本文介绍了InvalidOperationException:序列在AdalTokenCache.cs中不包含任何元素ADALTokenCache.BeforeAccessNotification(TokenCacheNotificationArgs args):60的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 希望我在适当的社区发布。 我在访问时遇到以下错误MVC5网站,是为内部目的而开发的。此站点为用户使用o365身份验证,并在第一次进行身份验证时工作但是在应用程序池回收/重新启动后第一次访问时,用户会收到此错误。通常在早上第一次访问时发生,我相信应用程序池在晚上被回收 [InvalidOperationException:Sequence不包含任何元素] System.Linq.Enumerable.First(IEnumerable`1 source)+451 C:\\\\\\\\\\\\\\\\ :12px;行高:18px;保证金底部:0px"> Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache.OnBeforeAccess(TokenCacheNotificationArgs args)+95 Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows。< StoreResultExToCacheAsync> d__54.MoveNext()+92 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+60 Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows。< RunAsync> d__53.MoveNext()+ 3747 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+60 Microsoft.IdentityModel.Clients.ActiveDirectory。< AcquireTokenByAuthorizationCodeCommonAsync> d__35.MoveNext()+ 451 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+60 Microsoft.IdentityModel.Clients.ActiveDirectory。< AcquireTokenByAuthorizationCodeAsync> d__51.MoveNext()+ 368 堆栈跟踪表示第60行(标记为 粗体下面的)创建MVC项目时生成的文件。 特别是在申请时池已被回收,我可以通过回收池重现错误。不知道如何解决。我特此附上cs 档 谢谢, 神圣 使用System; 使用System.Collections.Generic; 使用System.ComponentModel.DataAnnotations; 使用System.Data.Entity; 使用System.Linq; 使用System.Web;使用System.Web.Security ; 使用Microsoft.IdentityModel.Clients.ActiveDirectory; 名称空间Web.Models {公共类ADALTokenCache:TokenCache { private ApplicationDbContext db = new ApplicationDbContext(); private string userId; 私有UserTokenCache缓存; public ADALTokenCache(string signedInUserId) { //将缓存关联到网络应用的当前用户 userId = signedInUserId; this.AfterAccess = AfterAccessNotification; this.BeforeAccess = BeforeAccessNotification; this.BeforeWrite = BeforeWriteNotification; //查找数据库中的条目 Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); //将条目放入内存 try { this.Deserialize((Cache == null)?null:MachineKey.Unprotect(Cache.cacheBits," ADALCache" )); } catch(System.Security.Cryptography.CryptographicException cge) { db.UserTokenCacheList.Remove(Cache); db.SaveChanges(); } } //清理数据库 public override void Clear() { base.Clear(); var cacheEntry = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); db.UserTokenCacheList.Remove(cacheEntry); db.SaveChanges(); } //在ADAL访问缓存之前引发的通知。 //如果内存版本过时,这是你从数据库更新内存中副本的机会 void BeforeAccessNotification(TokenCacheNotificationArgs args) { if( Cache == null) { //第一次访问 Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); } else { //从DB 中检索最后一次写入 var status =来自db.UserTokenCacheList中的e 其中(e .webUserUniqueId == userId)选择新的 { LastWrite = e.LastWrite }; //如果in-内存副本早于持久副本 if(status.First()。LastWrite> Cache.LastWrite) { //从存储中读取,更新内存中副本 Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); } } this.Deserialize((Cache == null)?null:MachineKey.Unprotect(Cache.cacheBits," ADALCache")); } // ADAL访问缓存后引发的通知。 //如果设置了HasStateChanged标志,ADAL更改了缓存的内容 void AfterAccessNotification(TokenCacheNotificationArgs args) { //如果状态改变了 if( this.HasStateChanged) { if(Cache == null) { Cache = new UserTokenCache { webUserUniqueId = userId } ; } Cache.cacheBits = MachineKey.Protect(this.Serialize()," ADALCache"); Cache.LastWrite = DateTime.Now; //更新数据库和lastwrite db.Entry(Cache).State = Cache.UserTokenCacheId == 0? EntityState.Added:EntityState.Modified; db.SaveChanges(); this.HasStateChanged = false; } } void BeforeWriteNotification(TokenCacheNotificationArgs args) { //如果你想确保不发生并发写入,请使用此通知锁定条目} public override void DeleteItem(TokenCacheItem item) { base.DeleteItem(item); } } } 解决方案 任何人都可以请更新这个问题?很奇怪,没有人在MVC项目生成的脚手架代码上遇到这个问题。 Hi,Hope I post on the appropriate community.I am getting the following error when I am accessing a MVC5 site which has been developed for internal purposes. This site uses o365 authentication for the users and works fine while authenticating for the first time However the first access after the application pool recycle / restart, the user gets this error. Usually happens on the first access on the morning and I believe in the night the application pool gets recycled[InvalidOperationException: Sequence contains no elements] System.Linq.Enumerable.First(IEnumerable`1 source) +451 Web.Models.ADALTokenCache.BeforeAccessNotification(TokenCacheNotificationArgs args) in C:\source\AdalTokenCache.cs:60 Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache.OnBeforeAccess(TokenCacheNotificationArgs args) +95 Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.<StoreResultExToCacheAsync>d__54.MoveNext() +92 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.<RunAsync>d__53.MoveNext() +3747 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 Microsoft.IdentityModel.Clients.ActiveDirectory.<AcquireTokenByAuthorizationCodeCommonAsync>d__35.MoveNext() +451 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 Microsoft.IdentityModel.Clients.ActiveDirectory.<AcquireTokenByAuthorizationCodeAsync>d__51.MoveNext() +368 The stack trace indicates the line 60 (marked inBold down below) of file generated while creating the MVC project. This happens especially when an application pool has been recycled and I can able to reproduce the error by recycling the pool. Not sure how to resolve. I hereby attach cs fileThanks,Holyusing System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Data.Entity;using System.Linq;using System.Web;using System.Web.Security;using Microsoft.IdentityModel.Clients.ActiveDirectory;namespace Web.Models{ public class ADALTokenCache : TokenCache { private ApplicationDbContext db = new ApplicationDbContext(); private string userId; private UserTokenCache Cache; public ADALTokenCache(string signedInUserId) { // associate the cache to the current user of the web app userId = signedInUserId; this.AfterAccess = AfterAccessNotification; this.BeforeAccess = BeforeAccessNotification; this.BeforeWrite = BeforeWriteNotification; // look up the entry in the database Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); // place the entry in memory try { this.Deserialize((Cache == null) ? null : MachineKey.Unprotect(Cache.cacheBits, "ADALCache")); } catch (System.Security.Cryptography.CryptographicException cge) { db.UserTokenCacheList.Remove(Cache); db.SaveChanges(); } } // clean up the database public override void Clear() { base.Clear(); var cacheEntry = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); db.UserTokenCacheList.Remove(cacheEntry); db.SaveChanges(); } // Notification raised before ADAL accesses the cache. // This is your chance to update the in-memory copy from the DB, if the in-memory version is stale void BeforeAccessNotification(TokenCacheNotificationArgs args) { if (Cache == null) { // first time access Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); } else { // retrieve last write from the DB var status = from e in db.UserTokenCacheList where (e.webUserUniqueId == userId) select new { LastWrite = e.LastWrite }; // if the in-memory copy is older than the persistent copy if (status.First().LastWrite > Cache.LastWrite) { // read from from storage, update in-memory copy Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId); } } this.Deserialize((Cache == null) ? null : MachineKey.Unprotect(Cache.cacheBits, "ADALCache")); } // Notification raised after ADAL accessed the cache. // If the HasStateChanged flag is set, ADAL changed the content of the cache void AfterAccessNotification(TokenCacheNotificationArgs args) { // if state changed if (this.HasStateChanged) { if (Cache == null) { Cache = new UserTokenCache { webUserUniqueId = userId }; } Cache.cacheBits = MachineKey.Protect(this.Serialize(), "ADALCache"); Cache.LastWrite = DateTime.Now; // update the DB and the lastwrite db.Entry(Cache).State = Cache.UserTokenCacheId == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); this.HasStateChanged = false; } } void BeforeWriteNotification(TokenCacheNotificationArgs args) { // if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry } public override void DeleteItem(TokenCacheItem item) { base.DeleteItem(item); } }} 解决方案 Could anybody update on this issue please? It's so strange that nobody gets this problem on the scaffolding code generated by MVC Project. 这篇关于InvalidOperationException:序列在AdalTokenCache.cs中不包含任何元素ADALTokenCache.BeforeAccessNotification(TokenCacheNotificationArgs args):60的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 21:40