C# 多租户Azure AD身份验证权限错误

C# 多租户Azure AD身份验证权限错误,c#,asp.net-mvc,azure,azure-active-directory,azure-ad-graph-api,C#,Asp.net Mvc,Azure,Azure Active Directory,Azure Ad Graph Api,我是Azure广告认证新手。我已经在azure中创建了一个应用程序,并使其成为多租户,并将其权限设置如下 登录并读取用户配置文件 读取目录数据 这是我的Startup.Auth.cscode public partial class Startup { private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; private string app

我是Azure广告认证新手。我已经在azure中创建了一个应用程序,并使其成为多租户,并将其权限设置如下

  • 登录并读取用户配置文件

  • 读取目录数据

这是我的
Startup.Auth.cs
code

public partial class Startup
    {
        private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
        private string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
        private string graphResourceID = "https://graph.windows.net";
        private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
        private string authority = aadInstance + "common";
        private ApplicationDbContext db = new ApplicationDbContext();

        public void ConfigureAuth(IAppBuilder app)
        {

            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions { });

            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = clientId,
                    Authority = authority,
                    TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                    {
                        // instead of using the default validation (validating against a single issuer value, as we do in line of business apps), 
                        // we inject our own multitenant validation logic
                        ValidateIssuer = false,
                    },
                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        SecurityTokenValidated = (context) => 
                        {
                            return Task.FromResult(0);
                        },
                        AuthorizationCodeReceived = (context) =>
                        {
                            var code = context.Code;

                            ClientCredential credential = new ClientCredential(clientId, appKey);
                            string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
                            string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;

                            AuthenticationContext authContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
                            AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                                code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceID);

                            return Task.FromResult(0);
                        },
                        AuthenticationFailed = (context) =>
                        {
                            context.OwinContext.Response.Redirect("/Home/Error");
                            context.HandleResponse(); // Suppress the exception
                            return Task.FromResult(0);
                        }
                    }
                });

        }
    }
但当我尝试运行应用程序并登录时,它会给我错误信息

    You can't access this application 
    XXXXXXX needs permission to access resources in your organization that only an admin can grant. 
    Please ask an admin to grant permission to this app before you can use it.

    Have an admin account? Sign in with that account 
    Return to the application without granting consent 

管理员必须首先授予权限,以便其他用户能够访问资源。尝试以下步骤

  • 以管理员身份登录门户
  • 转到你的应用程序注册刀片
  • 单击所需权限
  • 在顶部的权限刀片中,单击
    授予权限
    链接
  • 阅读确认消息并单击“确定”
  • 现在尝试使用非管理员用户登录


    希望有帮助。

    谢谢您的回复。我按照你的建议做了同样的事,但还是犯了同样的错误。我正在尝试登录admin帐户,因为目前admin是目录中唯一的用户。您是否能够使用相同的帐户登录门户,并能够使用相同的clientId设置此应用程序的权限?是的,一切正常。我正在为此使用不同的浏览器。在一个浏览器中,我以管理员身份登录azure门户,并使用另一个浏览器尝试运行appOk。我认为您正在尝试访问
    图形资源
    。您是否向应用程序授予了对
    Microsoft Graph
    资源API的访问权限?如果没有,请在
    所需的任务中添加
    Microsoft Graph
    API。实际上
    Visual Studio 2015
    自己创建了所有这些代码。当我们尝试在VS 2015中创建一个新项目时,它提供了身份验证选项,然后在azure中创建整个代码和应用程序。这里我们只使用azure graph。