Example #1
0
        private static int TrySessionAuthentificate(Guid sessionId)
        {
            //По умолчанию не используем утентификацию из сессии если пользователь аутентифицирован
            int retVal = Security.CurrentUser != null ? Security.CurrentUser.UserID : -1;

            if (retVal == -1)
            {
                //Пытаемся получить id пользователя из сессии
                retVal = WebDavAuthHelper.GetUserIdByAuthToken(sessionId);
                if (retVal != -1)
                {
                    //аутентифицируем пользователя из сессии
                    UserLight user = UserLight.Load(retVal);
                    Security.SetCurrentUser(user);
                    //Refresh auth session
                    RefreshAuthSession(sessionId);
                }
            }
            return(retVal);
        }
Example #2
0
        internal static string GetWebDavUrl(WebDavAbsolutePath absPath, bool withAuthToken,
                                            bool detectServerEdit, Uri baseUrl)
        {
            if (absPath == null)
            {
                throw new ArgumentNullException("absPath");
            }
            if (baseUrl == null)
            {
                throw new ArgumentNullException("baseUrl");
            }

            Guid?  authToken       = null;
            bool?  bWebDavTurnOn   = PortalConfig.UseWebDav;
            string applicationPath = baseUrl.AbsolutePath;

            if (HttpContext.Current != null)
            {
                //try get cached token
                authToken       = (Guid?)HttpContext.Current.Items[AUTH_TOKEN_CACHE_NAME];
                applicationPath = HttpContext.Current.Request.ApplicationPath;
                applicationPath = applicationPath.TrimEnd('/');
            }

            ePluginToken pluginToken = ePluginToken.files;

            //Determine server editing
            if (detectServerEdit)
            {
                if (bWebDavTurnOn.HasValue && bWebDavTurnOn.Value && ContentTypeResolver.IsWebDAVSupportedExtension(Path.GetExtension(absPath.FileName)))
                {
                    pluginToken = ePluginToken.webdav;
                }
            }
            //Формировать authToken only for webdav resources
            if (pluginToken == ePluginToken.webdav)
            {
                //Использовать из кеша если нет то сгенерировать новый
                if (authToken == null)
                {
                    //authToken = withAuthToken ? WebDavAuthHelper.MakeAuthSession(true, absPath.StorageType, absPath.UniqueId) : Guid.Empty;
                    authToken = withAuthToken ? WebDavAuthHelper.MakeAuthSession(true, ObjectTypes.File_FileStorage, 0) : Guid.Empty;
                    if (HttpContext.Current != null && authToken != Guid.Empty)
                    {
                        //add to cache auth token
                        HttpContext.Current.Items[AUTH_TOKEN_CACHE_NAME] = authToken;
                    }
                }
            }
            else if (pluginToken == ePluginToken.files)
            {
                //never add auth token to file plugin token resources
                authToken = Guid.Empty;
            }

            WebDavTicket ticket     = WebDavTicket.CreateInstance(pluginToken, authToken.Value, absPath);
            UriBuilder   uriBuilder = new UriBuilder();

            uriBuilder.Scheme = baseUrl.Scheme;
            uriBuilder.Port   = baseUrl.Port;
            uriBuilder.Host   = baseUrl.Host;


            uriBuilder.Path = applicationPath + ticket.ToString();

            //Outer url to redirect page only for webdav access type
            if (ticket.PluginToken == ePluginToken.webdav && !withAuthToken)
            {
                uriBuilder.Path = applicationPath + AUTH_REDIRECT_PAGE;
                string webDavTicket = ticket.ToString("A");
                //Remove file name from ticket
                uriBuilder.Query = AUTH_TICKET_PARAM_NAME + String.Format("={0}", webDavTicket);
            }

            ////FileName = System.Web.HttpUtility.UrlPathEncode(FileName);

            return(Uri.EscapeUriString(uriBuilder.Uri.ToString()));
        }