C# ';当局';应为Uri格式参数名称:authority

C# ';当局';应为Uri格式参数名称:authority,c#,asp.net-mvc,azure,azure-active-directory,adal,C#,Asp.net Mvc,Azure,Azure Active Directory,Adal,我基于以下示例开发了我的mvc应用程序: Azure AAD的身份验证工作非常完美,我可以看到用户已登录: 然而,在下面的控制器中,我想打印出一些应用程序属性,我得到了上面的错误 “authority”应为Uri格式参数名称:authority 描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源 异常详细信息:System.ArgumentException:“权限”应位于 Uri格式参数名称:authority 控制器中的代

我基于以下示例开发了我的mvc应用程序:

Azure AAD的身份验证工作非常完美,我可以看到用户已登录:

然而,在下面的控制器中,我想打印出一些应用程序属性,我得到了上面的错误

“authority”应为Uri格式参数名称:authority 描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源

异常详细信息:System.ArgumentException:“权限”应位于 Uri格式参数名称:authority

控制器中的代码如下所示:

using Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.IdentityModel.Protocols;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace PruebasAD.Controllers
{
    public class ActiveDirectoryController : Controller
    {
        private static string azureAdGraphApiEndPoint = ConfigurationManager.AppSettings["ida:AzureAdGraphApiEndPoint"];
        private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
        private static string appKey = ConfigurationManager.AppSettings["ida:AppKey"];

        // GET: ActiveDirectory
        public ActionResult GetAzureAadApp()
        {
            // Instantiate an instance of ActiveDirectoryClient.
            Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);
            ActiveDirectoryClient adClient = new ActiveDirectoryClient(
                serviceRoot,
                async () => await GetAppTokenAsync());

            // Create the extension property
            string extPropertyName = "VehInfo";
            ExtensionProperty extensionProperty = new ExtensionProperty()
            {
                Name = extPropertyName,
                DataType = "String",
                TargetObjects = { "User" }
            };

            Application app =(Application)adClient.Applications.Where(
                    a => a.AppId == clientId).ExecuteSingleAsync().Result;

            if (app == null)
            {
                throw new ApplicationException("Unable to get a reference to application in Azure AD.");
            }

            return View(app);
        }

        private static async Task<string> GetAppTokenAsync()
        {
            string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
            string appKey = ConfigurationManager.AppSettings["ida:AppKey"];
            string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
            string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
            string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
            string azureAdGraphApiEndPoint = ConfigurationManager.AppSettings["ida:AzureAdGraphApiEndPoint"];
            // This is the resource ID of the AAD Graph API.  We'll need this to request a token to call the Graph API.
            string graphResourceId = ConfigurationManager.AppSettings["ida:GraphResourceId"];

            string Authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

            // Instantiate an AuthenticationContext for my directory (see authString above).
            AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance, false);

            // Create a ClientCredential that will be used for authentication.
            // This is where the Client ID and Key/Secret from the Azure Management Portal is used.
            ClientCredential clientCred = new ClientCredential(clientId, appKey);

            // Acquire an access token from Azure AD to access the Azure AD Graph (the resource)
            // using the Client ID and Key/Secret as credentials.
            AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(azureAdGraphApiEndPoint, clientCred);

            // Return the access token.
            return authenticationResult.AccessToken;
        }
    }


    public class CompanyInfo
    {
        public int Nit;
        public string Nombre;
    }
}
使用Microsoft.Azure.ActiveDirectory.GraphClient;
使用Microsoft.IdentityModel.Clients.ActiveDirectory;
使用Microsoft.IdentityModel.Protocols;
使用制度;
使用System.Collections.Generic;
使用系统配置;
利用制度全球化;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Web;
使用System.Web.Mvc;
命名空间PruebasAD.Controllers
{
公共类ActiveDirectoryController:控制器
{
私有静态字符串azureAdGraphApiEndPoint=ConfigurationManager.AppSettings[“ida:azureAdGraphApiEndPoint”];
私有静态字符串clientId=ConfigurationManager.AppSettings[“ida:clientId”];
私有静态字符串appKey=ConfigurationManager.AppSettings[“ida:appKey”];
//获取:ActiveDirectory
公共行动结果GetAzureAadApp()
{
//实例化ActiveDirectoryClient的实例。
Uri serviceRoot=新Uri(AzureAdGraphHapiendPoint);
ActiveDirectoryClient adClient=新的ActiveDirectoryClient(
serviceRoot,
async()=>等待GetAppTokenAsync());
//创建扩展属性
字符串extPropertyName=“VehInfo”;
ExtensionProperty ExtensionProperty=新的ExtensionProperty()
{
Name=extPropertyName,
DataType=“String”,
targetObject={“用户”}
};
应用程序app=(应用程序)adClient.Applications.Where(
a=>a.AppId==clientId).ExecuteSingleAsync().Result;
如果(app==null)
{
抛出新的ApplicationException(“无法在Azure AD中获取对应用程序的引用”);
}
返回视图(app);
}
专用静态异步任务GetAppTokenAsync()
{
字符串clientId=ConfigurationManager.AppSettings[“ida:clientId”];
字符串appKey=ConfigurationManager.AppSettings[“ida:appKey”];
字符串aadInstance=ConfigurationManager.AppSettings[“ida:aadInstance”];
字符串tenant=ConfigurationManager.AppSettings[“ida:tenant”];
字符串postLogoutRedirectUri=ConfigurationManager.AppSettings[“ida:postLogoutRedirectUri”];
字符串azureAdGraphApiEndPoint=ConfigurationManager.AppSettings[“ida:azureAdGraphApiEndPoint”];
//这是AAD图形API的资源ID。我们需要它来请求一个令牌来调用图形API。
字符串graphResourceId=ConfigurationManager.AppSettings[“ida:graphResourceId”];
string Authority=string.Format(CultureInfo.InvariantCulture,aadInstance,tenant);
//为我的目录实例化AuthenticationContext(请参见上面的authString)。
AuthenticationContext AuthenticationContext=新的AuthenticationContext(aadInstance,false);
//创建将用于身份验证的ClientCredential。
//这是使用Azure管理门户中的客户端ID和密钥/机密的地方。
ClientCredential clientCred=新的ClientCredential(clientId,appKey);
//从Azure AD获取访问令牌以访问Azure AD图(资源)
//使用客户端ID和密钥/机密作为凭据。
AuthenticationResult AuthenticationResult=等待authenticationContext.AcquireTokenAsync(AzureAdGraphHapiendPoint,clientCred);
//返回访问令牌。
返回authenticationResult.AccessToken;
}
}
公共类公司信息
{
公共int Nit;
公共字符串名称;
}
}
还有web.config,为了安全起见做了一些更改

<add key="ida:GraphResourceId" value="https://graph.windows.net" />
    <add key="ida:GraphUserUrl" value="https://graph.windows.net/{0}/me?api-version=2013-11-08" />
    <add key="ida:ClientId" value="xx-b1aa-42ab-9693-6c22d01ca338" />
    <add key="ida:AppKey" value="xx/6Vsq0CuhQyYVcR5Vggw=" />
    <add key="ida:Tenant" value="xx.onmicrosoft.com" />
    <add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}" />
    <add key="ida:PostLogoutRedirectUri" value="https://localhost:44300/" />
    <add key="ida:AzureAdGraphApiEndPoint" value="https://graph.windows.net/xx-d5f0-453b-8f60-2be9b41b2ea0" />

您需要将
权限
传递给
AuthenticationContext()
而不是
aadInstance

// Instantiate an AuthenticationContext for my directory (see authString above).
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);