コード例 #1
0
    [InlineData(null)]      // null
    public void ReturnExpectedIdentifier(string staticIdentifier)
    {
        var strategy = new StaticMultiTenantStrategy(staticIdentifier);

        var identifier = strategy.GetIdentifier(new Object());

        Assert.Equal(staticIdentifier, identifier);
    }
    public void GetTenantFromStore()
    {
        var store = CreateTestStore();

        var strat    = new StaticMultiTenantStrategy("initech");
        var resolver = new TenantResolver(store, strat);
        var tc       = resolver.ResolveAsync(null).Result;

        Assert.Equal("initech", tc.Id);
        Assert.Equal("initech", tc.Identifier);
        Assert.Equal("Initech", tc.Name);
        Assert.Equal(typeof(StaticMultiTenantStrategy), tc.MultiTenantStrategyType);
        Assert.Equal(typeof(InMemoryMultiTenantStore), tc.MultiTenantStoreType);
    }
コード例 #3
0
        private static async Task <TenantContext> ResolveForRemoteAuthentication(HttpContext context)
        {
            var schemes  = context.RequestServices.GetService <IAuthenticationSchemeProvider>();
            var handlers = context.RequestServices.GetService <IAuthenticationHandlerProvider>();

            foreach (var scheme in await schemes.GetRequestHandlerSchemesAsync())
            {
                // Check to see if this handler would apply and resolve tenant context if so.
                // Hanlders have a method, ShouldHandleAsync, which would be nice here, but it causes issues
                // with caching.
                // Workaround is to copy the logic from ShouldHandleAsync which requires instantiating options the hard way.

                var optionType         = scheme.HandlerType.GetProperty("Options").PropertyType;
                var optionsFactoryType = typeof(IOptionsFactory <>).MakeGenericType(optionType);
                var optionsFactory     = context.RequestServices.GetRequiredService(optionsFactoryType);
                var options            = optionsFactoryType.GetMethod("Create").Invoke(optionsFactory, new[] { scheme.Name }) as RemoteAuthenticationOptions;

                if (options.CallbackPath == context.Request.Path)
                {
                    // Skip if this is not a compatible type of authentication.
                    if (!(options is OAuthOptions || options is OpenIdConnectOptions))
                    {
                        continue;
                    }

                    try
                    {
                        string state = null;

                        if (string.Equals(context.Request.Method, "GET", StringComparison.OrdinalIgnoreCase))
                        {
                            state = context.Request.Query["state"];
                        }
                        else if (string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase) &&
                                 !string.IsNullOrEmpty(context.Request.ContentType) &&
                                 context.Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase) &&
                                 context.Request.Body.CanRead)
                        {
                            var formOptions = new FormOptions {
                                BufferBody = true
                            };
                            var form = await context.Request.ReadFormAsync(formOptions);

                            state = form.Where(i => i.Key.ToLowerInvariant() == "state").Single().Value;
                        }

                        var oAuthOptions         = options as OAuthOptions;
                        var openIdConnectOptions = options as OpenIdConnectOptions;

                        var properties = oAuthOptions?.StateDataFormat.Unprotect(state) ??
                                         openIdConnectOptions?.StateDataFormat.Unprotect(state);

                        if (properties.Items.Keys.Contains("tenantIdentifier"))
                        {
                            var tenantIdentifier = properties.Items["tenantIdentifier"];

                            var strategy = new StaticMultiTenantStrategy(tenantIdentifier);
                            var store    = context.RequestServices.GetRequiredService <IMultiTenantStore>();
                            var resolver = new TenantResolver(store, strategy);

                            return(await resolver.ResolveAsync(context));
                        }
                    }
                    catch (Exception e)
                    {
                        throw new MultiTenantException("Error occurred resolving tenant for remote authentication.", e);
                    }
                }
            }

            return(null);
        }