2020-08-11 16:06:37 +10:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System;
|
2020-06-19 12:04:16 +10:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-22 15:08:54 +10:00
|
|
|
|
using System.Linq;
|
2020-08-11 16:06:37 +10:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Web;
|
2019-03-28 12:10:59 +01:00
|
|
|
|
using System.Web.Mvc;
|
2020-08-11 16:06:37 +10:00
|
|
|
|
using Umbraco.Core;
|
2020-06-19 12:04:16 +10:00
|
|
|
|
using Umbraco.Web.Security;
|
2019-03-28 12:10:59 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ViewDataExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string TokenUmbracoPath = "UmbracoPath";
|
|
|
|
|
|
public const string TokenInstallApiBaseUrl = "InstallApiBaseUrl";
|
|
|
|
|
|
public const string TokenUmbracoBaseFolder = "UmbracoBaseFolder";
|
|
|
|
|
|
public const string TokenExternalSignInError = "ExternalSignInError";
|
|
|
|
|
|
public const string TokenPasswordResetCode = "PasswordResetCode";
|
|
|
|
|
|
|
|
|
|
|
|
public static bool FromTempData(this ViewDataDictionary viewData, TempDataDictionary tempData, string token)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (tempData[token] == null) return false;
|
|
|
|
|
|
viewData[token] = tempData[token];
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-11 16:06:37 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Copies data from a request cookie to view data and then clears the cookie in the response
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="viewData"></param>
|
|
|
|
|
|
/// <param name="httpContext"></param>
|
|
|
|
|
|
/// <param name="cookieName"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// <para>
|
|
|
|
|
|
/// This is similar to TempData but in some cases we cannot use TempData which relies on the temp data provider and session.
|
|
|
|
|
|
/// The cookie value can either be a simple string value
|
|
|
|
|
|
/// </para>
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
internal static bool FromBase64CookieData<T>(this ViewDataDictionary viewData, HttpContextBase httpContext, string cookieName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var hasCookie = httpContext.Request.HasCookie(cookieName);
|
|
|
|
|
|
if (!hasCookie) return false;
|
|
|
|
|
|
|
|
|
|
|
|
// get the cookie value
|
|
|
|
|
|
var cookieVal = httpContext.Request.GetCookieValue(cookieName);
|
|
|
|
|
|
|
|
|
|
|
|
if (cookieVal == null)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// ensure the cookie is expired (must be done after reading the value)
|
|
|
|
|
|
httpContext.ExpireCookie(cookieName);
|
|
|
|
|
|
|
|
|
|
|
|
if (cookieVal.IsNullOrWhiteSpace())
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(System.Net.WebUtility.UrlDecode(cookieVal)));
|
|
|
|
|
|
// deserialize to T and store in viewdata
|
|
|
|
|
|
viewData[cookieName] = JsonConvert.DeserializeObject<T>(decoded);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-28 12:10:59 +01:00
|
|
|
|
public static string GetUmbracoPath(this ViewDataDictionary viewData)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (string)viewData[TokenUmbracoPath];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetUmbracoPath(this ViewDataDictionary viewData, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
viewData[TokenUmbracoPath] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetInstallApiBaseUrl(this ViewDataDictionary viewData)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (string)viewData[TokenInstallApiBaseUrl];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetInstallApiBaseUrl(this ViewDataDictionary viewData, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
viewData[TokenInstallApiBaseUrl] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetUmbracoBaseFolder(this ViewDataDictionary viewData)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (string)viewData[TokenUmbracoBaseFolder];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetUmbracoBaseFolder(this ViewDataDictionary viewData, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
viewData[TokenUmbracoBaseFolder] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-19 11:24:03 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used by the back office login screen to get any registered external login provider errors
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="viewData"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2020-06-19 12:04:16 +10:00
|
|
|
|
public static BackOfficeExternalLoginProviderErrors GetExternalSignInProviderErrors(this ViewDataDictionary viewData)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (BackOfficeExternalLoginProviderErrors)viewData[TokenExternalSignInError];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Obsolete("Use GetExternalSignInProviderErrors instead")]
|
2019-03-28 12:10:59 +01:00
|
|
|
|
public static IEnumerable<string> GetExternalSignInError(this ViewDataDictionary viewData)
|
|
|
|
|
|
{
|
2020-06-19 12:04:16 +10:00
|
|
|
|
var errs = viewData.GetExternalSignInProviderErrors();
|
2020-06-22 15:08:54 +10:00
|
|
|
|
return errs?.Errors ?? Enumerable.Empty<string>();
|
2019-03-28 12:10:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-19 11:24:03 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used by the back office controller to register any external login provider errors
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="viewData"></param>
|
2020-06-19 12:04:16 +10:00
|
|
|
|
/// <param name="errors"></param>
|
|
|
|
|
|
public static void SetExternalSignInProviderErrors(this ViewDataDictionary viewData, BackOfficeExternalLoginProviderErrors errors)
|
|
|
|
|
|
{
|
|
|
|
|
|
viewData[TokenExternalSignInError] = errors;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Obsolete("Use SetExternalSignInProviderErrors instead")]
|
2019-03-28 12:10:59 +01:00
|
|
|
|
public static void SetExternalSignInError(this ViewDataDictionary viewData, IEnumerable<string> value)
|
|
|
|
|
|
{
|
2020-06-19 12:04:16 +10:00
|
|
|
|
viewData[TokenExternalSignInError] = new BackOfficeExternalLoginProviderErrors(string.Empty, value);
|
2019-03-28 12:10:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetPasswordResetCode(this ViewDataDictionary viewData)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (string)viewData[TokenPasswordResetCode];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetPasswordResetCode(this ViewDataDictionary viewData, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
viewData[TokenPasswordResetCode] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|