2017-03-21 5 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環境から「マルチテナント」キーを取得していますが、私はそれが私のOwinContext環境IEの「マルチテナント」キーが表示されないApplicationOAuthProviderクラスから同じことをフェッチしよう:以下GetEnvironment関数変数:

私は私のコントローラ内部でそれを得るのに対し、私はApplicationOAuthProviderのOwinContext.Environmentで「マルチテナント」キーを取得していないです、なぜ誰もが
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 

を知っていますか?

ありがとうございます!

答えて

0

あなたの各apiコントローラに注入されたコンテキストを使用して、テナントとそのコンテキストがレイヤー全体で見えるようにすることをお勧めします。コンテキストプロバイダは、次にDIフレームワークのコンテキスト・プロバイダを登録するこの

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

ような何かを探していることができる[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

恐ろしいサラバナン..同じものの詳細ドキュメントを提供してくれてありがとう。感謝します ! –

+0

@TarunOhri:それが役に立つと分かったら、答えとしてマークしてください – Saravanan

関連する問題