我想實現我的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而我明白了我的控制器內?
謝謝!
真棒Saravanan ..感謝爲同提供詳細的文檔。欣賞它! –
@TarunOhri:如果你覺得有用,請不要將其標記爲一個答案 – Saravanan