0

上記のサービスをXamarinフォームで使用すると、OAuth(MicrosoftとGoogle)で認証を有効にできます。 APIコールがうまく機能しており、[Authorize]を使用しているこれらの機能が認証で強制されています。Azure Mobile Appサービスの役割認証が機能しない

ただし、特定のロールを定義すると、401エラーが返されます。自分自身の属性をAuthorizationFilterAttributeクラスから作成しようとしています。 のUserProfileUserRoles

私は私のカスタムテーブルでのユーザー情報とロールを格納しています。したがって、これらを定義する場所はわかりません。これらの表からデータを読み取っていないためです。私が次のことを実行しようとすると、空の結果が返されます。

[Authorize] 
    [HttpGet] 
    public IHttpActionResult Roles() 
    { 
     ClaimsIdentity claimsId = ClaimsPrincipal.Current.Identity as ClaimsIdentity; 
     var appRoles = new List<String>(); 
     foreach (Claim claim in ClaimsPrincipal.Current.FindAll(claimsId.RoleClaimType)) 
      appRoles.Add(claim.Value); 

     return Ok(appRoles); 
    } 

私は、Web APIをもとにして出会った最も近い記事:Web Api 2 and Owin

編集エイドリアンによって回答に基づいて、私は次のコードを追加、まだ同じ401エラーを取得します。

public class MyAuthorize : System.Web.Http.Filters.AuthorizationFilterAttribute 
{ 
    private string[] accessRoleNames; 

    public MyAuthorize(params string[] roleNames) 
    { 
     accessRoleNames = roleNames; 
    } 

    public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken) 
    { 
     try 
     { 
      await Task.Delay(100); 
      OnAuthorization(actionContext); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 

    public override void OnAuthorization(HttpActionContext actionContext) 
    { 
     WMSEntities db = new WMSEntities(); 

     string userID = actionContext.ControllerContext.RequestContext.Principal.Identity.Name; 
     var user = db.UserProfiles.FirstOrDefault(x => x.User_ID == userID); 

     if (user == null) 
      actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); 
     else 
     { 
      bool ok = false; 
      foreach (var item in user.AppRoles) 
      { 
       foreach (string ar in accessRoleNames) 
       { 
        if (item.Name == ar) 
         ok = true; 
       } 
      } 
      db.Dispose(); 

      if (!ok) 
       actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); 
     } 
     base.OnAuthorization(actionContext); 
    } 
} 

そして、次のように私の関数に参照:

[MyAuthorize(UserRole.Admin, UserRole.Collector)] 
    [HttpGet] 
    [Route("GetDataAdmin")] 
    public string GetDataAdmin() 
    { 
     return "success access GetDataAdmin"; 
    } 

私はローカルコードをデバッグする場合は、OnAuthorizationイベントは、したがって、これをトラブルシューティングする方法がわからないヒットされていません。

答えて

1

Azure Mobile Appsでは、EntityFrameworkを使用しています。あなたはこれを望むすべてのクエリを行うことができます。ここではAAD資格によって返されたクレームをチェックする例AuthorizationFilterAttributeです:あなたはモデルとしてこれを使用することができます

[AuthorizeClaims("groups","some-object-id")] 

using System.Linq; 
using System.Net; 
using System.Security.Principal; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Web.Http; 
using System.Web.Http.Controllers; 
using System.Web.Http.Filters; 
using Microsoft.Azure.Mobile.Server.Authentication; 

namespace Backend.Helpers 
{ 
    public class AuthorizeClaimsAttribute : AuthorizationFilterAttribute 
    { 
     string Type { get; } 
     string Value { get; } 

     public AuthorizeClaimsAttribute(string type, string value) 
     { 
      Type = type; 
      Value = value; 
     } 

     public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken) 
     { 
      var request = actionContext.Request; 
      var user = actionContext.RequestContext.Principal; 
      if (user != null) 
      { 
       var identity = await user.GetAppServiceIdentityAsync<AzureActiveDirectoryCredentials>(request); 
       var countOfMatchingClaims = identity.UserClaims 
        .Where(c => c.Type.Equals(Type) && c.Value.Equals(Value)) 
        .Count(); 
       if (countOfMatchingClaims > 0) return; 

      } 
      throw new HttpResponseException(HttpStatusCode.Unauthorized); 
     } 
    } 
} 

この特定のバージョンは、次のようなことを行うことができます。 GetAppServiceIdentityAsync()をチェックすると、代わりにデータベースをチェックして、プリンシパルに役割があるかどうかを判断できます。

+0

私は上記を試みましたが、運もありません。それでも401エラーが発生し、イベントがヒットしていないため、ローカルでデバッグすることはできません。 –

+0

私のコードでいくつかの訂正が動作します: var identity = actionContext.ControllerContext.RequestContext.Principal.Identity as ClaimsIdentity; クレームidentityClaim = identity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier); var user = db.UserProfiles.FirstOrDefault(x => x.User_ID.EndsWith(identityClaim.Value)); ' –

関連する問題