Ejemplo n.º 1
0
        private void Setup()
        {
            var tenant = CoreContext.TenantManager.GetCurrentTenant();

            var user = SecurityContext.CurrentAccount;

            _log.Debug("ApiHelper->Setup: Tenant={0} User='******' IsAuthenticated={2} Scheme='{3}' HttpContext is {4}",
                       tenant.TenantId, user.ID, user.IsAuthenticated, Scheme,
                       HttpContext.Current != null
                          ? string.Format("not null and UrlRewriter = {0}, RequestUrl = {1}", HttpContext.Current.Request.GetUrlRewriter(), HttpContext.Current.Request.Url)
                          : "null");

            if (!user.IsAuthenticated)
            {
                throw new AuthenticationException("User not authenticated");
            }

            var hs = new HostedSolution(ConfigurationManager.ConnectionStrings["default"]);

            var authenticationCookie = hs.CreateAuthenticationCookie(tenant.TenantId, user.ID);

            var tempUrl = (WebConfigurationManager.AppSettings["api.url"] ?? "").Trim('~', '/');

            var ubBase = new UriBuilder
            {
                Scheme = Scheme,
                Host   = tenant.GetTenantDomain(false)
            };

            var virtualDir = WebConfigurationManager.AppSettings["api.virtual-dir"];

            if (!string.IsNullOrEmpty(virtualDir))
            {
                tempUrl = string.Format("{0}/{1}", virtualDir.Trim('/'), tempUrl);
            }

            var host = WebConfigurationManager.AppSettings["api.host"];

            if (!string.IsNullOrEmpty(host))
            {
                ubBase.Host = host;
            }

            var port = WebConfigurationManager.AppSettings["api.port"];

            if (!string.IsNullOrEmpty(port))
            {
                ubBase.Port = int.Parse(port);
            }

            ubBase.Path = tempUrl;

            BaseUrl = ubBase;

            _cookie = new Cookie("asc_auth_key", authenticationCookie, "/", BaseUrl.Host);
        }