c# - “Authorization has been denied for this request” 对于使用 Azure Active Directory 的少数用户

标签 c# azure owin adfs

我有一个 native iOS 应用程序,具有 Azure ADFS 的单点登录身份验证。启动应用程序时,用户必须登录到 Azure Active Directory,并在成功进行身份验证后获取 token ,以便使用 Web API 验证所有服务端点调用。
授权对于少数用户来说非常有效(例如开发人员登录) ),但对于组织内的一些 AD 登录,Web API 返回此请求的授权已被拒绝。 为了保持简单,我从这个等式中删除了应用程序,并尝试使用 Postman 来访问在 header 中传递授权 token 的 Web API。
对于少数帐户,Web API 会成功返回数据,并且对于少数帐户来说,Web API 会成功返回数据。帐户,抛出此请求的授权已被拒绝。 (401 未经授权)
鉴于身份验证引擎对少数帐户成功工作,我想知道这是否是 Active Directory 用户角色/组策略的问题。
有谁遇到过这个问题或者知道这个问题的原因吗?对此的任何帮助将不胜感激。

更多详细信息:

  • WebAPI 2.0 托管在 Azure 中。
  • 在 Web API StartUp 类中

        var azureActiveDirectoryBearerAuthenticationOptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
            AuthenticationType = "OAuth2Bearer",
    
            Tenant = "companyName.onmicrosoft.com",
            TokenValidationParameters = new TokenValidationParameters
            {
                ValidAudience = ConfigurationManager.AppSettings["ida:AppIdUri"],
            },
            Provider = new OAuthBearerAuthenticationProvider()
            {
                OnRequestToken = (context) =>
                {
                    OAuthRequestTokenContext token = context;
                    System.Diagnostics.Debug.WriteLine(token.Token);
                    return Task.FromResult(0);   
                },  
                OnApplyChallenge = (context) =>
                {
                    OAuthChallengeContext challenge = context;
                    var types = challenge.OwinContext.Authentication.GetAuthenticationTypes();
                    foreach (var type in types)
                    {
                        System.Diagnostics.Debug.WriteLine(type.Caption + " "+ type.AuthenticationType);
                    }
                    return Task.FromResult(0);
                },
                OnValidateIdentity = (context) =>
                {
                    var authenticationTicket = context.Ticket;
                    var claims = ClaimsHelper.GetClaimsFor(authenticationTicket.Identity.Name);
                    context.Ticket.Identity.AddClaims(claims);
    
                    return Task.FromResult(0);
                }
            }
    
        };
    

    `

OAuthBearerAuthenticationProvider 中的 OWIN 身份验证类型是 OAuth2Bearer 和 Federation
此时不涉及基于角色的声明。

客户端生成这样的 token ,并将其作为 header 中的承载 token 在每个请求上传递

AuthenticationContext authenticationContext = new AuthenticationContext(azureSettings.AdAuthority);
AuthenticationResult authenticationResult = 
await authenticationContext.AcquireTokenAsync(azureSettings.AdResource,
                          azureSettings.AdClientId,
                          new Uri(azureSettings.AdRedirect),
                          new AuthorizationParameters(view),
                          UserIdentifier.AnyUser,
                          string.Format("domain_hint={0}", azureSettings.AdDomainHint));

`

Azure Active Directory 与本地 Active Directory 同步。

尝试了以下方法但没有成功:

创建一个新的 AD 用户,其角色和部门与可以成功检索数据并同步到 AAD 的用户相同,甚至新用户会获得未经授权。确保 Azure 上的 Web API 不限于单个指定用户。

最佳答案

终于把这个问题搞清楚了。 ADFS 的 ADAL 设置或 OAuth2.0 身份验证没有任何问题。调用确实进行到 OnValidateIdentity = (context) =>,这意味着设置一切正常。

问题出在代码中,该代码具有检查属于特定声明的用户的逻辑。 var Claims = ClaimsHelper.GetClaimsFor(authenticationTicket.Identity.Name);

ClaimsHelper.GetClaimsFor方法的深处,

 if (containsRole == false)
                        {
                            throw new AppDefinedException("You are not configured with any roles for access to the product, for identity: " + userId);
                        }

由于授权失败并引发了异常,因此该异常会向上冒泡到 OWIN 中间件,并返回默认(通用)401 Unauthorized 异常。但由于某种原因,OWIN 中间件从未传递异常详细信息(甚至没有作为内部异常传递)。

我对一般异常感到困惑,即 AD 用户角色的设置有问题,甚至一开始就没有进行身份验证。

注意:

如果我们自己的代码故意拒绝授权,那么记录(数据库或文件系统)将是一个好主意。

我仍在研究拒绝请求的最佳方法,以将更有意义的信息传回应用程序。如果您知道处理此问题的最佳方法,请告诉我。

另外,附注:即使在配置文件中启用了日志监听器,Katana 仅在抛出任何异常时才会记录到 .Net 跟踪。

提示:查看 jwt 是否未损坏的最佳方法 - jwt.io

感谢那些试图提供帮助的人。

关于c# - “Authorization has been denied for this request” 对于使用 Azure Active Directory 的少数用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858909/

相关文章:

c# - 发送不记名 token 时 API 端点返回 "Authorization has been denied for this request."

c# - Visual Studio 找不到的文件关联中使用的图标

python - 如何使用Python检查Azure容器中是否存在文件夹?

azure - 无法 ping 我的 aci - azure 容器实例的公共(public) IP

azure - 如何获取Azure Active Directory应用程序的权限?

oauth - 使用 ASP.net API 的不记名 token 身份验证

c# - 检查开始和结束日期

c# - 如何使用 DISTINCT 在 NHibernate SQL 查询中进行分页

c# - 获取 Entity Framework 中的总行数

c# - 使用来自桌面应用程序的 MVC 身份代码