c# - How to bind a property to a complex object using WebApi and a custom value binder? -


i have different web api controllers secured using claims. so, in controllers, i'm using asp.net identity , user.identity of type claimsidentity.

one of claims current userid logged application.

when i'm posting complex object order instance , order has property userid following example, i'd property filled value in claim.

class order {   public int quantity { get; set; }   public int productid { get; set; }   public int userid { get; set; } }  class orderscontroller : baseapicontroller  {   [httppost]   public ihttpactionresult post(order order)   {     //     return ok();   } } 

so, want have quantity , producid properties filled values coming body of request, , userid claims.

i've tried creating custom value provider following:

public class identityclaimsvalueprovider : ivalueprovider {     private readonly dictionary<string, string> _claims;     private readonly string[] _claimskeys;      public identityclaimsvalueprovider(dictionary<string, string> claims)     {         _claims = claims;         _claimskeys = claims.keys.toarray();     }      public bool containsprefix(string prefix)     {         if (prefix == null)             throw new argumentnullexception(nameof(prefix));          return _claimskeys.any(x => prefix.equals(x, stringcomparison.ordinalignorecase));     }      public valueproviderresult getvalue(string key)     {         if (key == null)             throw new argumentnullexception(nameof(key));          var claim = _claimskeys.firstordefault(x => key.equals(x, stringcomparison.ordinalignorecase));         if (claim != null && _claims[claim] != null)         {             return new valueproviderresult(_claims[claim], _claims[claim], cultureinfo.currentculture);         }          return null;     } }  public class identityclaimsvalueproviderfactory : valueproviderfactory {     public override ivalueprovider getvalueprovider(httpactioncontext actioncontext)     {         var controller = actioncontext.controllercontext.controller webapibasecontroller;         if (controller == null)             return null;          var claims = controller.getuseridentity().claims;          var claimsvalues = new dictionary<string, string>         {             { "userid", claims.first(x => x.type == common.constants.claimtypes.user_id).value }         };          return new identityclaimsvalueprovider(claimsvalues);     } } 

i added config:

config.services.add(typeof(valueproviderfactory), new identityclaimsvalueproviderfactory()); 

but not work. when make post, have quantity , productid properties filled, not userid

you must change signature of action :

public ihttpactionresult post([valueprovider(typeof(identityclaimsvalueproviderfactory))]string userid, order order) 

if want have value of useid in order property, must change class identityclaimsvalueproviderfactory

regards.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -