2017-12-12 4 views
0

Azure ADを使用するC#アプリケーションを作成しました。以下は私のStartup.Auth.csファイルです。私がドメインに接続しているときは、すべて正常に動作します。しかし、ドメインにいないときにUser.IsInRoleを使用すると、信頼関係エラーが発生します。何が原因だろうか?Azure AD RBAC User.IsInRoleがドメインにないときに信頼関係エラーをスローする

さらに:を使用してください。[Authorize(Roles="MyRole")] WORKS! 正確なエラーは次のとおりです。The trust relationship between this workstation and the primary domain failed.

// Startup.Auth.cs 
    private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; 
    private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; 
    private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"]; 
    private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"]; 
    private static string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"]; 

    string authority = aadInstance + tenantId; 

    public void ConfigureAuth(IAppBuilder app) 
    { 
     app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

     app.UseOpenIdConnectAuthentication(
      new OpenIdConnectAuthenticationOptions 
      { 
       ClientId = clientId, 
       Authority = authority, 
       PostLogoutRedirectUri = postLogoutRedirectUri, 
       RedirectUri = redirectUri, 
       TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() 
       { 
        ValidateIssuer = true, // For Single-Tenant App. 
        RoleClaimType = "roles" // Grab roles when user authenticates. 
       }, 

       Notifications = new OpenIdConnectAuthenticationNotifications() 
       { 
        AuthenticationFailed = (context) => 
        { 
         return System.Threading.Tasks.Task.FromResult(0); 
        } 
       } 

      }); 
     // This makes any middleware defined above this line run before the Authorization rule is applied in web.config 
     app.UseStageMarker(PipelineStage.Authenticate); 
    } 

答えて

0

私は最終的にこれを考え出しました。ここで私は私のコントローラで何をやっていたの例です:

var entitiesToDisplay = db.myEntities 
    .where(x => x.RequiredRole == string.empty || User.IsInRole(x.RequiredRole); 

私はこれを変更:

IEnumerable<Entities> entitiesToDisplay; 
if (Request.IsAuthenticated) { 
    entitiesToDisplay = db.myEntities 
    .where(x => x.RequiredRole == string.empty || User.IsInRole(x.RequiredRole); 
} 
else { 
    entitiesToDisplay = new List(); 
} 

最終的には、ときRequest.IsAuthenticated私は真実ではないUser.IsInRoleが例外をスローすることが判明しました。チェックしてRequest.IsAuthenticated私の問題を解決しました。

関連する問題