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

时间:2017-07-15 14:15:17

标签: c# asp.net-mvc azure azure-active-directory azure-ad-graph-api

我是Azure AD身份验证的新手。我已经在azure中创建了一个应用程序并使其成为多租户并将其权限设置为以下

  • 登录并阅读用户个人资料

  • 读取目录数据

这是我的Startup.Auth.cs代码

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 

1 个答案:

答案 0 :(得分:1)

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

  1. 以管理员身份登录门户网站
  2. 转到您的App注册刀片
  3. 点击Required Permissions
  4. 在顶部的权限刀片中点击Grant Permissions链接。
  5. 阅读确认信息并单击“确定”。
  6. 现在尝试使用非管理员用户登录。

    希望this article有所帮助。