2017-03-21 21 views
0

我想實現我的WebAPI項目多租戶。OwinContext環境不含有添加元素

在我Startup.Auth.cs,我將選擇租戶對象爲IOwinContext。

 app.Use(async (ctx, next) => 
     { 
      Tenant tenant = GetTenantBasedUrl(ctx.Request.Uri.Host); 
      if (tenant == null) 
      { 
       throw new ApplicationException("tenant not found"); 
      } 
      ctx.Environment.Add("MultiTenant", tenant); 
      await next(); 
     } 

其中,GetTenantBaseUrl函數返回選定的Tenant對象。 我做了,我會實現礦井的每個控制器,以獲得租戶對象的類實現ApiController。

public class MultiTenantWebApiController : ApiController 
{ 
    public Tenant Tenant 
    { 
     get 
     { 
      object multiTenant; 
      IDictionary<string, object> dic = HttpContext.Current.GetOwinContext().Environment; 
      if (!HttpContext.Current.GetOwinContext().Environment.TryGetValue("MultiTenant", out multiTenant)) 
      { 
       throw new ApplicationException("Could Not Find Tenant"); 
      } 
      return (Tenant)multiTenant; 
     } 
    } 

} 

在我的控制器我收到來自OwinContext環境「多租戶」鍵,但我嘗試獲取來自ApplicationOAuthProvider類同它不顯示在我的OwinContext環境即「多租戶」鍵:下面getEnvironment變量:

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider 
{ 
    private readonly string _publicClientId; 

// some code here 

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     try 
     { 
      **IDictionary getEnvironment = HttpContext.Current.GetOwinContext().Environment;** 
     // some code 

是否有人知道爲什麼我沒有得到的「多租戶」鍵ApplicationOAuthProvider的OwinContext.Environment而我明白了我的控制器內?

謝謝!

回答

0

我想建議你可以使用注入到每個API控制器的情況下讓房客和其背景是跨層可見。上下文提供可以看起來像這樣

public class ClaimsContextDataProvider : IUserContextDataProvider 
    { 
     public Guid UserId 
     { 
      get 
      { 
       var userId = (Thread.CurrentPrincipal as ClaimsPrincipal)?.FindFirst(ClaimTypes.Sid)?.Value; 
       return TryGetGuidFromString(userId); 
      } 
     } 
} 

然後登記在DI框架上下文提供[例如使用Autofac的下面給出]

builder.RegisterType<ClaimsContextDataProvider>().As<IUserContextDataProvider>();

然後具有BaseApiController像下面的代碼段

public Guid TenantId { get { return _userContext.TenantId; } } 

     public BaseApiController(IMapper mapper, IUserContextDataProvider userContext) 
     { 
      _mapper = mapper; 
      _userContext = userContext; 
     } 

訪問衍生C內的BaseApiController的TenantId屬性ontrollers [CountriesController.cs]

// POST api/countries 
     public async Task<HttpResponseMessage> PostCountry(CountryRequestModel requestModel) 
     { 
      Country country = _mapper.Map<CountryRequestModel, Country>(requestModel); 
      country.CreatedOn = DateTimeOffset.Now; 
      country.TenantId = TenantId; 

      await _countryService.AddAsync(country); 

      CountryDto countryDto = _mapper.Map<Country, CountryDto>(country); 
      HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, countryDto); 
      response.Headers.Location = GetCountryLink(country.Id); 

      return response; 
     } 

你可以看看示例應用程序,並在下面的鏈接

Multi-Tenant dev template

其給出的模版有點深,在這裏解釋,請隨時免費閱讀的文檔here

+0

真棒Saravanan ..感謝爲同提供詳細的文檔。欣賞它! –

+0

@TarunOhri:如果你覺得有用,請不要將其標記爲一個答案 – Saravanan