# Conflicts: # build/NuSpecs/UmbracoCms.Core.nuspec # build/NuSpecs/UmbracoCms.Web.nuspec # src/SolutionInfo.cs # src/Umbraco.Core/Cache/CacheKeys.cs # src/Umbraco.Core/Composing/TypeFinder.cs # src/Umbraco.Core/Configuration/GlobalSettings.cs # src/Umbraco.Core/Configuration/GlobalSettingsExtensions.cs # src/Umbraco.Core/Configuration/IGlobalSettings.cs # src/Umbraco.Core/Configuration/UmbracoSettings/ContentElement.cs # src/Umbraco.Core/Configuration/UmbracoSettings/ContentSectionExtensions.cs # src/Umbraco.Core/Constants-AppSettings.cs # src/Umbraco.Core/Editors/UserEditorAuthorizationHelper.cs # src/Umbraco.Core/Extensions/StringExtensions.cs # src/Umbraco.Core/Extensions/UriExtensions.cs # src/Umbraco.Core/IO/IOHelper.cs # src/Umbraco.Core/IO/PhysicalFileSystem.cs # src/Umbraco.Core/Media/Exif/MathEx.cs # src/Umbraco.Core/Media/UploadAutoFillProperties.cs # src/Umbraco.Core/Models/Mapping/UserMapDefinition.cs # src/Umbraco.Core/Models/Membership/User.cs # src/Umbraco.Core/Models/UserExtensions.cs # src/Umbraco.Core/Packaging/PackageDefinitionXmlParser.cs # src/Umbraco.Core/PropertyEditors/ListViewConfiguration.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/MediaPickerValueConverter.cs # src/Umbraco.Core/PropertyEditors/ValueConverters/MultiNodeTreePickerValueConverter.cs # src/Umbraco.Core/Routing/AliasUrlProvider.cs # src/Umbraco.Core/Routing/DefaultUrlProvider.cs # src/Umbraco.Core/Routing/UriUtility.cs # src/Umbraco.Core/Routing/UrlProviderExtensions.cs # src/Umbraco.Core/Runtime/CoreRuntime.cs # src/Umbraco.Core/RuntimeOptions.cs # src/Umbraco.Core/RuntimeState.cs # src/Umbraco.Core/Security/BackOfficeUserStore.cs # src/Umbraco.Core/Security/ContentPermissions.cs # src/Umbraco.Core/Sync/ApplicationUrlHelper.cs # src/Umbraco.Core/Trees/TreeNode.cs # src/Umbraco.Core/Udi.cs # src/Umbraco.Examine.Lucene/BackOfficeExamineSearcher.cs # src/Umbraco.Examine/Umbraco.Examine.csproj # src/Umbraco.Infrastructure/Examine/ContentValueSetValidator.cs # src/Umbraco.Infrastructure/Migrations/Install/DatabaseBuilder.cs # src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs # src/Umbraco.Infrastructure/Persistence/SqlSyntax/SqlServerSyntaxProvider.cs # src/Umbraco.Infrastructure/Scoping/Scope.cs # src/Umbraco.Infrastructure/Search/ExamineComponent.cs # src/Umbraco.Infrastructure/Security/IdentityMapDefinition.cs # src/Umbraco.Infrastructure/Services/Implement/ContentService.cs # src/Umbraco.Infrastructure/Services/Implement/MediaService.cs # src/Umbraco.Infrastructure/Services/Implement/NotificationService.cs # src/Umbraco.Persistence.SqlCe/SqlCeSyntaxProvider.cs # src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/LocksTests.cs # src/Umbraco.Tests.UnitTests/Umbraco.Core/Models/UserExtensionsTests.cs # src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Editors/UserEditorAuthorizationHelperTests.cs # src/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Examine/UmbracoContentValueSetValidatorTests.cs # src/Umbraco.Tests/Configurations/UmbracoSettings/ContentElementTests.cs # src/Umbraco.Tests/Configurations/UmbracoSettings/umbracoSettings.config # src/Umbraco.Tests/TestHelpers/SettingsForTests.cs # src/Umbraco.Tests/Testing/TestDatabase.cs # src/Umbraco.Tests/Web/Controllers/ContentControllerUnitTests.cs # src/Umbraco.Tests/Web/Controllers/FilterAllowedOutgoingContentAttributeTests.cs # src/Umbraco.Tests/Web/Controllers/MediaControllerUnitTests.cs # src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs # src/Umbraco.Web.BackOffice/Controllers/CodeFileController.cs # src/Umbraco.Web.BackOffice/Controllers/ContentController.cs # src/Umbraco.Web.BackOffice/Controllers/EntityController.cs # src/Umbraco.Web.BackOffice/Controllers/MacrosController.cs # src/Umbraco.Web.BackOffice/Controllers/MediaController.cs # src/Umbraco.Web.BackOffice/Controllers/PackageInstallController.cs # src/Umbraco.Web.BackOffice/Controllers/TourController.cs # src/Umbraco.Web.BackOffice/Controllers/UserGroupEditorAuthorizationHelper.cs # src/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingContentAttribute.cs # src/Umbraco.Web.BackOffice/Filters/FilterAllowedOutgoingMediaAttribute.cs # src/Umbraco.Web.BackOffice/Mapping/ContentMapDefinition.cs # src/Umbraco.Web.BackOffice/Services/IconService.cs # src/Umbraco.Web.BackOffice/Trees/ContentTreeController.cs # src/Umbraco.Web.BackOffice/Trees/ContentTreeControllerBase.cs # src/Umbraco.Web.BackOffice/Trees/FileSystemTreeController.cs # src/Umbraco.Web.BackOffice/Trees/MediaTreeController.cs # src/Umbraco.Web.Common/Extensions/FormCollectionExtensions.cs # src/Umbraco.Web.UI.Client/src/common/resources/content.resource.js # src/Umbraco.Web.UI.Client/src/views/content/overlays/publish.html # src/Umbraco.Web.UI.Client/src/views/propertyeditors/grid/editors/media.controller.js # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/da.xml # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en.xml # src/Umbraco.Web.UI.NetCore/umbraco/config/lang/en_us.xml # src/Umbraco.Web.UI/config/umbracoSettings.Release.config # src/Umbraco.Web/Cache/MemberCacheRefresher.cs # src/Umbraco.Web/Composing/ModuleInjector.cs # src/Umbraco.Web/Editors/BackOfficeController.cs # src/Umbraco.Web/Editors/Binders/ContentModelBinderHelper.cs # src/Umbraco.Web/Editors/ContentTypeController.cs # src/Umbraco.Web/Editors/Filters/ContentSaveValidationAttribute.cs # src/Umbraco.Web/Editors/Filters/MediaItemSaveValidationAttribute.cs # src/Umbraco.Web/Editors/Filters/UserGroupAuthorizationAttribute.cs # src/Umbraco.Web/Editors/TinyMceController.cs # src/Umbraco.Web/Editors/UserGroupsController.cs # src/Umbraco.Web/Editors/UsersController.cs # src/Umbraco.Web/ImageCropperTemplateExtensions.cs # src/Umbraco.Web/Logging/WebProfiler.cs # src/Umbraco.Web/Logging/WebProfilerProvider.cs # src/Umbraco.Web/Macros/PublishedContentHashtableConverter.cs # src/Umbraco.Web/Mvc/EnsurePublishedContentRequestAttribute.cs # src/Umbraco.Web/Mvc/JsonNetResult.cs # src/Umbraco.Web/Mvc/MemberAuthorizeAttribute.cs # src/Umbraco.Web/Mvc/RenderRouteHandler.cs # src/Umbraco.Web/PropertyEditors/MediaPickerPropertyEditor.cs # src/Umbraco.Web/PropertyEditors/MultiNodeTreePickerPropertyEditor.cs # src/Umbraco.Web/PublishedCache/NuCache/DataSource/DatabaseDataSource.cs # src/Umbraco.Web/RoutableDocumentFilter.cs # src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs # src/Umbraco.Web/Routing/NotFoundHandlerHelper.cs # src/Umbraco.Web/Routing/PublishedRouter.cs # src/Umbraco.Web/Runtime/WebInitialComposer.cs # src/Umbraco.Web/Scheduling/KeepAlive.cs # src/Umbraco.Web/Security/AppBuilderExtensions.cs # src/Umbraco.Web/Security/BackOfficeClaimsIdentityFactory.cs # src/Umbraco.Web/Security/Providers/UmbracoMembershipProvider.cs # src/Umbraco.Web/Trees/DictionaryTreeController.cs # src/Umbraco.Web/Trees/LanguageTreeController.cs # src/Umbraco.Web/Trees/LogViewerTreeController.cs # src/Umbraco.Web/Trees/PackagesTreeController.cs # src/Umbraco.Web/UmbracoApplication.cs # src/Umbraco.Web/UmbracoApplicationBase.cs # src/Umbraco.Web/UmbracoInjectedModule.cs # src/Umbraco.Web/WebApi/Filters/AdminUsersAuthorizeAttribute.cs # src/Umbraco.Web/WebApi/Filters/CheckIfUserTicketDataIsStaleAttribute.cs # src/Umbraco.Web/WebApi/Filters/EnsureUserPermissionForContentAttribute.cs # src/Umbraco.Web/WebApi/Filters/EnsureUserPermissionForMediaAttribute.cs # src/Umbraco.Web/WebApi/MemberAuthorizeAttribute.cs
177 lines
6.4 KiB
C#
177 lines
6.4 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Web;
|
|
using Umbraco.Extensions;
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
|
|
|
namespace Umbraco.Web
|
|
{
|
|
/// <summary>
|
|
/// Extension methods used to check/set cookie values
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This should 100% supersede the StateManager.Cookies
|
|
/// </remarks>
|
|
internal static class HttpCookieExtensions
|
|
{
|
|
/// <summary>
|
|
/// Retrieves an individual cookie from the cookies collection
|
|
/// </summary>
|
|
/// <param name="requestHeaders"></param>
|
|
/// <param name="cookieName"></param>
|
|
/// <returns></returns>
|
|
/// <remarks>
|
|
/// Adapted from: https://stackoverflow.com/a/29057304/5018 because there's an issue with .NET WebApi cookie parsing logic
|
|
/// when using requestHeaders.GetCookies() when an invalid cookie name is present.
|
|
/// </remarks>
|
|
public static string GetCookieValue(this HttpRequestHeaders requestHeaders, string cookieName)
|
|
{
|
|
foreach (var header in requestHeaders)
|
|
{
|
|
if (header.Key.Equals("Cookie", StringComparison.InvariantCultureIgnoreCase) == false)
|
|
continue;
|
|
|
|
var cookiesHeaderValue = header.Value.FirstOrDefault();
|
|
if (cookiesHeaderValue == null)
|
|
return null;
|
|
|
|
var cookieCollection = cookiesHeaderValue.Split(Constants.CharArrays.Semicolon, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (var cookieNameValue in cookieCollection)
|
|
{
|
|
var parts = cookieNameValue.Split(Constants.CharArrays.EqualsChar, StringSplitOptions.RemoveEmptyEntries);
|
|
if (parts.Length != 2) continue;
|
|
if (parts[0].Trim().Equals(cookieName, StringComparison.InvariantCultureIgnoreCase))
|
|
return parts[1].Trim();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the cookie from the request and the response if it exists
|
|
/// </summary>
|
|
/// <param name="http"></param>
|
|
/// <param name="cookieName"></param>
|
|
public static void ExpireCookie(this HttpContextBase http, string cookieName)
|
|
{
|
|
//remove from the request
|
|
http.Request.Cookies.Remove(cookieName);
|
|
|
|
//expire from the response
|
|
var cookie = http.Response.Cookies[cookieName];
|
|
if (cookie != null)
|
|
{
|
|
//this will expire immediately and be removed from the browser
|
|
cookie.Expires = DateTime.Now.AddYears(-1);
|
|
}
|
|
else
|
|
{
|
|
//ensure there's def an expired cookie
|
|
http.Response.Cookies.Add(new HttpCookie(cookieName) { Expires = DateTime.Now.AddYears(-1) });
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the cookie from the request and the response if it exists
|
|
/// </summary>
|
|
/// <param name="http"></param>
|
|
/// <param name="cookieName"></param>
|
|
public static void ExpireCookie(this HttpContext http, string cookieName)
|
|
{
|
|
new HttpContextWrapper(http).ExpireCookie(cookieName);
|
|
}
|
|
|
|
public static string GetPreviewCookieValue(this HttpRequestMessage request)
|
|
{
|
|
var cookie = request.Headers.GetCookies(Constants.Web.PreviewCookieName).FirstOrDefault();
|
|
if (cookie != null)
|
|
{
|
|
if (cookie[Constants.Web.PreviewCookieName] != null)
|
|
{
|
|
return cookie[Constants.Web.PreviewCookieName].Value;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the cookie's string value
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cookieName"></param>
|
|
/// <returns></returns>
|
|
public static string GetCookieValue(this HttpRequestBase request, string cookieName)
|
|
{
|
|
var cookie = request.Cookies.Get(cookieName);
|
|
if (cookie != null)
|
|
{
|
|
if (cookie.Value.IsNullOrWhiteSpace() == false)
|
|
{
|
|
return cookie.Value;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the cookie's string value
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cookieName"></param>
|
|
/// <returns></returns>
|
|
public static string GetCookieValue(this HttpRequest request, string cookieName)
|
|
{
|
|
return new HttpRequestWrapper(request).GetCookieValue(cookieName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Does a cookie exist with the specified key ?
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static bool HasCookie(this HttpRequestBase request, string key)
|
|
{
|
|
return request.Cookies[key] != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Does a cookie exist with the specified key ?
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static bool HasCookie(this HttpRequest request, string key)
|
|
{
|
|
return request.Cookies[key] != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Is there a cookie with the key supplied and does it have a value that is not empty
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static bool HasCookieValue(this HttpRequestBase request, string key)
|
|
{
|
|
return request.Cookies[key] != null
|
|
&& request.Cookies[key].Value != null
|
|
&& request.Cookies[key].Value.IsNullOrWhiteSpace() == false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Is there a cookie with the key supplied and does it have a value that is not empty
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static bool HasCookieValue(this HttpRequest request, string key)
|
|
{
|
|
return new HttpRequestWrapper(request).HasCookieValue(key);
|
|
}
|
|
}
|
|
}
|