ASP.NET无法删除自身DataProtection密钥,.NET9 Blazor项目启动遇权限错误
解决OSX下Rider调试.NET9 Blazor项目时DataProtection权限报错问题
你遇到的报错信息如下:
Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[48] An error occurred while reading the key ring. System.UnauthorizedAccessException: Access to the path '/Users/me/.aspnet/DataProtection-Keys/1961c782-5584-4ea5-b2ca-aab7be755266.tmp' is denied. ---> System.IO.IOException: Operation not permitted --- End of inner exception stack trace --- at System.IO.FileSystem.DeleteFile(String fullPath)
以下是几种可行的解决办法:
检查Rider的系统权限
打开系统设置→隐私与安全性→完全磁盘访问权限,确认Rider已被添加并勾选权限。若未找到,点击加号,从Applications目录中选择Rider添加,之后重启Rider重试。自定义DataProtection密钥存储路径
在项目的Program.cs中配置项目内的目录存储密钥,避开系统目录的权限限制:var keyDir = Path.Combine(builder.Environment.ContentRootPath, "DataProtectionKeys"); Directory.CreateDirectory(keyDir); builder.Services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo(keyDir));重置密钥目录的所有者与权限
在终端执行以下命令(替换me为你的实际用户名):sudo chown -R $USER:$USER /Users/me/.aspnet/DataProtection-Keys sudo chmod -R u+rwx /Users/me/.aspnet/DataProtection-Keys执行完成后重启Rider再调试项目。
检查Rider运行配置
打开Rider的运行配置(右上角运行按钮下拉→Edit Configurations),确认"Working directory"为项目根目录,未设置特殊环境变量或运行用户。也可尝试新建默认运行配置替换原有配置测试。临时关闭系统完整性保护(SIP,谨慎操作)
这是最后手段,重启Mac进入恢复模式(按住Command+R),打开终端执行:csrutil disable重启系统后测试,问题解决后建议重新开启SIP(恢复模式下执行
csrutil enable)
内容的提问来源于stack exchange,提问作者tonsteri
相关产品推荐
相关产品推荐

