2017-09-19 3 views
1

Azure에서 웹 응용 프로그램을 만들었습니다. AzureAD를 기반으로하는 인증을 받았습니다 ...웹 응용 프로그램에서 Azure 역할 얻기

실제로 모든 사용자는 동일한 권한을 가지고 있습니다 ... 나는 관리자 그룹과 다른 그룹의 그룹이 있어야합니다.

나는 enter image description here 내가 내 응용 프로그램에서 이러한 역할을 사용할 수 있습니다 ... 내 웹 응용 프로그램의 푸른 포털에서 어떤 역할이 나열되어 Acces control (IAM)이 있음을 볼 수? 나는 그 인증을 "클레임 기반"라는 제대로 이해하고

var isAdmin = User.HasClaim("IsAdmin", true.ToString()); 

,하지만 난 역할 기반 인증을 사용하려고하고 싶습니다 ... : 사실은 내가보기에 무엇을

입니다 ...

나는

var userIdentity = (System.Security.Claims.ClaimsIdentity)User.Identity; 
var claims = userIdentity.Claims; 
var roleClaimType = userIdentity.RoleClaimType; 
var roles = claims.Where(c => c.Type == System.Security.Claims.ClaimTypes.Role).ToList(); 

을 할 수도 시도했지만 역할 목록이 비어 있음 여기

난 당신이 포털에서 관찰 된 역할의 관리에 관련이 있다고 생각하여 public void Configure(IApplicationBuilder app,...

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions 
{ 
    ClientId = Configuration["Authentication:AzureAd:ClientId"], 
    Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"], 
    CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"], 
    Events = new OpenIdConnectEvents 
    { 
     OnTicketReceived = async context => 
     { 
      var user = (ClaimsIdentity)context.Ticket.Principal.Identity; 
      if (user.IsAuthenticated) 
      { 
       var firstName = user.FindFirst(ClaimTypes.GivenName).Value; 
       var lastName = user.FindFirst(ClaimTypes.Surname).Value; 
       var email = user.HasClaim(cl => cl.Type == ClaimTypes.Email) ? user.FindFirst(ClaimTypes.Email).Value : user.Name; 
       var connectedOn = DateTime.UtcNow; 
       var userId = user.Name; 
       var myUser = await repository.GetAsync<Connection>(userId); 
       if (myUser == null) 
       { 
        myUser = new Connection(userId) 
        { 
         FirstName = firstName, 
         LastName = lastName, 
         Email = email 
        }; 
       } 
       myUser.LastConnectedOn = connectedOn; 
       List<Connection> myList = new List<Connection>() { myUser }; 
       var results = await repository.InsertOrMergeAsync(myList); 
       Claim clm = new Claim("IsAdmin", myUser.IsAdmin.ToString(), ClaimValueTypes.Boolean); 
       user.AddClaim(clm); 
      } 
      return; 
     } 
     }, 
    } 
}); 

Startup.cs Autentication 코드 또한 내 appsettings.json

"Authentication": { 
    "AzureAd": { 
    "AADInstance": "https://login.microsoftonline.com/", 
    "CallbackPath": "/signin-oidc", 
    "ClientId": "xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx", 
    "Domain": "mysite.azurewebsites.net", 
    "TenantId": "xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx" 
    } 
} 

답변

1

입니다 웹 애플 리케이션, 그것이 노출 기능에 대한 권한이 아닙니다. 프로그래밍 방식으로 역할을 사용하려면 웹 응용 프로그램으로 배포 한 프로젝트에 해당하는 Azure AD 응용 프로그램에서 역할을 설정하는 방법을 설명하는 다음 샘플을 살펴 보는 것이 좋습니다. https://github.com/Azure-Samples/active-directory-dotnet-webapp-roleclaims

당신이 (컨트롤러의 코드에서) 페이지를 보호 할 수 있습니다이 방법을 사용하여 속성 : ``

[Authorize(Roles = "Admin, Observer, Writer, Approver")] 

https://github.com/Azure-Samples/active-directory-dotnet-webapp-roleclaims/blob/master/WebApp-RoleClaims-DotNet/Controllers/TasksController.cs#L17

또한 사용자가 가지고 테스트 할 수 있습니다 참조 주어진 역할 : if (User.IsInRole("Admin") || User.IsInRole("Writer")) { ... }

+0

나를위한 주요 질문은 그 역할을 만드는 방법이 될 것입니다 ... '$ approverRole = CreateAppRole -Name "Approver"-Description "승인자는 작업 상태를 변경할 수있는 능력이 있습니다."는 약간은 모호합니다. 위의 예제에서 – Serge

+0

은 Manifest와 web.config에 대해 이야기하고 있습니다. ASP.NET에는 그런 것이 없습니다. 핵심 응용 프로그램 ... 또한 잘 이해한다면 RoleManager는 .Core 프레임 워크 용으로 아직 구현되었습니다 ... 또한 AzureTable 저장소를 사용하고 Sql/CoreFramework를 사용하지 않으므로 IdentityUser를 사용할 수 없습니다 ... – Serge

+0

매니 페스트는 Azure 포털에서 찾을 수 있습니다. Azure Active Directory -> App 등록 -> App -> Manifest를 찾으십시오. – juunas