错误:在命名空间'System.Web'中找不到'Configuration'类型或命名空间名称

5
我正在尝试使用来自 https://learn.microsoft.com/en-us/azure/key-vault/key-vault-use-from-web-application 的以下代码。
//add these using statements
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading.Tasks;
using System.Web.Configuration;

//this is an optional property to hold the secret after it is retrieved
public static string EncryptSecret { get; set; }

//the method that will be provided to the KeyVaultClient
public static async Task<string> GetToken(string authority, string resource, string scope)
{
    var authContext = new AuthenticationContext(authority);
    ClientCredential clientCred = new ClientCredential(WebConfigurationManager.AppSettings["ClientId"],
            WebConfigurationManager.AppSettings["ClientSecret"]);
    AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);

    if (result == null)
        throw new InvalidOperationException("Failed to obtain the JWT token");

    return result.AccessToken;
}

我试图将上述代码添加到C#类库中的一个类中,为了解决错误,我尝试了以下线程中的答案:https://forums.asp.net/t/1205345.aspx?The+type+or+namespace+name+Configuration+does+not+exist+in+the+namespace+System+Web+。建议的答案是:您需要通过“添加引用”对话框添加System.Configuration DLL。右键单击“引用”,选择“添加引用”,然后在.NET选项卡下选择System.Configuration。
但是,当我试图添加System.Configuration DLL作为引用时,我在列表中看不到它。 请问我在哪里可以找到这个System.Configuration DLL呢?
================== 更新 ======================
添加程序集相关截图。没有添加System.Configuration程序集的选项:enter image description here enter image description here

可能是WebConfigurationManager和ConfigurationManager有什么区别?的重复问题。 - Camilo Terevinto
关于程序集。并搜索System.Configuration。 - D-Shih
@D-Shih,我添加了2个截图。我没有选项添加程序集。 - aBlaze
@CamiloTerevinto,我找不到在该主题中讨论如何将 System.Web.Configuration 导入项目的位置。 - aBlaze
你有两个问题:首先,.NET标准是基于NuGet的。你找不到任何引用。其次,WebConfigurationManager是为ASP.NET项目设计的,而你的项目不是。 - Camilo Terevinto
1个回答

5

原帖的评论者们都是正确的。 我在这里总结答案:

  1. 我需要创建一个.NET Framework类库,而不是.NET Standard类库,因为WebConfigurationManager仅适用于.NET Framework。 enter image description here

  2. 右键单击类库的引用 -> 添加引用 -> 程序集 -> 查找System.Web.dllSystem.Configuration.dll ->确定。

没有更多的错误!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接