2017-05-23 16 views
0

私のシナリオは以下の通りです:
Web APIを使用してWebメソッド上のセキュリティを管理する方法は?

私はモバイルアプリのためのWEB APIを作成しました。..

  • TOKENのCALL - それAUTHORIZEトークン成功を返す& barerトークン 詳細
  • GetCompanyDetails(int型ログインID)
  • GetCompanyCustomer(int companyId)

私は次のようにデータベース内のテーブルを持っている:

ログインテーブル:

ID| Username | Password | UserType | CreatedDate 
1 | Alex | P#ssword | Company | 2017-05-04 
2 | Jhon | [email protected] | Company | 2017-05-10 
3 | Rubby | R#$S3343| Customer | 2017-05-11 
4 | Moris | M#$353 | Customer | 2017-05-11 
5 | Febio | [email protected] | Customer | 2017-05-11 

CompanyDetails表:

ID | LoginID | CompanyName | Address | CreatedDate | Location 
5 | 1  | ALEX Company| Street 1| 2017-05-04 | USA 
7 | 2  | JHON INC | NJ, OPP PR market| 2017-05-10 | USA 

CustomerDetails表:

ID | LoginID | Address | CreatedDate | Location 
10 | 3  | Address 1| 2017-05-11 | USA 
12 | 4  | Address 2| 2017-05-11 | USA 
13 | 5  | Address 3| 2017-05-11 | USA 

CompanyCustomer表:

ID | CompanyID | CustomerID 
1 | 5  | 10 
1 | 5  | 12 
2 | 7  | 13 

私は会社の顧客を取得するメソッドを呼び出していた後、私は、APIを認可したら。 私は顧客を得るために企業IDを渡していますその時は...

[HttpGet] 
[Authorize(Roles=("Company")] 
public List<Customer> CompanyCustomer(int companyId) 
{ 
//Return the list of customer by companyId 

} 

私のポイントは、トークンが正当同じユーザーであるユーザーを確認する方法です。
が、私はその後、私はその後、まだそれ(7)companyCustomerを呼ぶことになった後、(5)それは はすべての顧客

  • を返しますCompanyCustomerを呼び出す

    • ALEX当社の企業ID = 5を要求したとし他の会社のすべての顧客を返す。

      リクエストされたユーザーがそのAPI発信者トークンを検出する方法はありますか?

    どのようにこの種のセキュリティを処理しますか?

  • +1

    は、クレーム認証です。 [Here](http://bitoftech.net/2015/03/31/asp-net-web-api-claims-authorization-with-asp-net-identity-2-1/)には、以下を使用するサンプルがありますASP.NET ID。 ASP.NET Identityを使用しない場合でも原則は同じです。 –

    答えて

    0
    public sealed class PrivateAttribute : Attribute, IAuthorizationFilter { 
        public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(
         HttpActionContext actionContext, 
         CancellationToken cancellationToken, 
         Func<Task<HttpResponseMessage>> continuation) { 
         var claimsPrincipal = actionContext.RequestContext.Principal as ClaimsPrincipal; 
         if (actionContext.RequestContext.RouteData.Values.ContainsKey(CONSTANTS.USER_ID_KEY) && claimsPrincipal != null) { 
          var requestedID = actionContext.RequestContext.RouteData.Values[CONSTANTS.USER_ID_KEY]; 
          if (claimsPrincipal.HasClaim(CONSTANTS.USER_ID_KEY, requestedID.ToString())) { 
           return continuation(); 
          } else { // someone is trying to get resources of another user 
           return whatever fail; 
          } 
         } else { // there is no {id} paramter in the route, nothing to do 
          return continuation(); 
         } 
        } 
    
        public bool AllowMultiple => false; 
    } 
    

    そして、認証時に:あなたが探しているものを

    public override async Task GrantResourceOwnerCredentials(
         OAuthGrantResourceOwnerCredentialsContext context) { 
         context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 
         using (var authRepo = AuthRepository.Create()) { 
          var findUserResult = await authRepo.FindUser(context.UserName, context.Password); 
          if (findUserResult == UserModel.NoUser) { 
           context.SetError("error", "User not found."); 
          } else { 
           var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
           identity.AddClaim(// this is important, [PrivateAttribute] relies on this 
            new Claim(CONSTANTS.USER_ID_KEY, findUserResult.ID.ToString())); 
           context.Validated(identity); 
          } 
         } 
        } 
    
    関連する問題