Blazor Hybrid / .NET MAUI support

This commit is contained in:
Shaun Walker
2022-08-11 17:09:32 -04:00
parent cf2adc7f6a
commit f96129fa37
75 changed files with 2244 additions and 41 deletions

View File

@ -315,7 +315,7 @@ namespace Oqtane.Controllers
// POST api/<controller>/login
[HttpPost("login")]
public async Task<User> Login([FromBody] User user)
public async Task<User> Login([FromBody] User user, bool setCookie, bool isPersistent)
{
User loginUser = new User { SiteId = user.SiteId, Username = user.Username, IsAuthenticated = false };
@ -358,6 +358,11 @@ namespace Oqtane.Controllers
loginUser.LastIPAddress = LastIPAddress;
_users.UpdateUser(loginUser);
_logger.Log(LogLevel.Information, this, LogFunction.Security, "User Login Successful {Username}", user.Username);
if (setCookie)
{
await _identitySignInManager.SignInAsync(identityuser, isPersistent);
}
}
else
{

View File

@ -32,11 +32,12 @@ namespace Oqtane.Infrastructure
else
{
// if there is http context
if (_httpContextAccessor.HttpContext != null)
var httpcontext = _httpContextAccessor.HttpContext;
if (httpcontext != null)
{
// legacy support for client api requests which would include the alias as a path prefix ( ie. {alias}/api/[controller] )
int aliasId;
string[] segments = _httpContextAccessor.HttpContext.Request.Path.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string[] segments = httpcontext.Request.Path.Value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (segments.Length > 1 && (segments[1] == "api" || segments[1] == "pages") && int.TryParse(segments[0], out aliasId))
{
alias = _aliasRepository.GetAliases().ToList().FirstOrDefault(item => item.AliasId == aliasId);
@ -45,13 +46,19 @@ namespace Oqtane.Infrastructure
// resolve alias based on host name and path
if (alias == null)
{
string name = _httpContextAccessor.HttpContext.Request.Host.Value + _httpContextAccessor.HttpContext.Request.Path;
string name = httpcontext.Request.Host.Value + httpcontext.Request.Path;
alias = _aliasRepository.GetAlias(name);
}
// if there is a match save it
if (alias != null)
{
alias.Protocol = (httpcontext.Request.IsHttps) ? "https://" : "http://";
alias.BaseUrl = "";
if (httpcontext.Request.Headers.ContainsKey("User-Agent") && httpcontext.Request.Headers["User-Agent"] == Shared.Constants.MauiUserAgent)
{
alias.BaseUrl = alias.Protocol + alias.Name;
}
_siteState.Alias = alias;
}
}

View File

@ -27,7 +27,7 @@
{
@(Html.AntiForgeryToken())
<component type="typeof(Oqtane.App)" render-mode="@Model.RenderMode" param-AntiForgeryToken="@Model.AntiForgeryToken" param-Runtime="@Model.Runtime" param-RenderMode="@Model.RenderMode.ToString()" param-VisitorId="@Model.VisitorId" param-RemoteIPAddress="@Model.RemoteIPAddress" param-AuthorizationToken="@Model.AuthorizationToken" />
<component type="typeof(Oqtane.App)" render-mode="@((RenderMode)Enum.Parse(typeof(RenderMode), Model.RenderMode, true))" param-AntiForgeryToken="@Model.AntiForgeryToken" param-Runtime="@Model.Runtime" param-RenderMode="@Model.RenderMode.ToString()" param-VisitorId="@Model.VisitorId" param-RemoteIPAddress="@Model.RemoteIPAddress" param-AuthorizationToken="@Model.AuthorizationToken" />
<div id="blazor-error-ui">
<environment include="Staging,Production">

View File

@ -8,7 +8,6 @@ using System.Reflection;
using Oqtane.Repository;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
@ -61,7 +60,7 @@ namespace Oqtane.Pages
public string AntiForgeryToken = "";
public string AuthorizationToken = "";
public string Runtime = "Server";
public RenderMode RenderMode = RenderMode.Server;
public string RenderMode = "ServerPrerendered";
public int VisitorId = -1;
public string RemoteIPAddress = "";
public string HeadResources = "";
@ -84,7 +83,7 @@ namespace Oqtane.Pages
if (_configuration.GetSection("RenderMode").Exists())
{
RenderMode = (RenderMode)Enum.Parse(typeof(RenderMode), _configuration.GetSection("RenderMode").Value, true);
RenderMode = _configuration.GetSection("RenderMode").Value;
}
// if framework is installed
@ -123,7 +122,7 @@ namespace Oqtane.Pages
}
if (!string.IsNullOrEmpty(site.RenderMode))
{
RenderMode = (RenderMode)Enum.Parse(typeof(RenderMode), site.RenderMode, true);
RenderMode = site.RenderMode;
}
if (site.FaviconFileId != null)
{
@ -242,7 +241,8 @@ namespace Oqtane.Pages
try
{
// get request attributes
string useragent = (Request.Headers[HeaderNames.UserAgent] != StringValues.Empty) ? Request.Headers[HeaderNames.UserAgent].ToString().Substring(0,256) : "(none)";
string useragent = (Request.Headers[HeaderNames.UserAgent] != StringValues.Empty) ? Request.Headers[HeaderNames.UserAgent] : "(none)";
useragent = (useragent.Length > 256) ? useragent.Substring(0, 256) : useragent;
string language = (Request.Headers[HeaderNames.AcceptLanguage] != StringValues.Empty) ? Request.Headers[HeaderNames.AcceptLanguage] : "";
language = (language.Contains(",")) ? language.Substring(0, language.IndexOf(",")) : language;
language = (language.Contains(";")) ? language.Substring(0, language.IndexOf(";")) : language;

View File

@ -112,7 +112,7 @@ namespace Oqtane.Repository
url = Utilities.ContentUrl(alias, file.FileId);
break;
case FolderTypes.Public:
url = "/" + Utilities.UrlCombine("Content", "Tenants", alias.TenantId.ToString(), "Sites", file.Folder.SiteId.ToString(), file.Folder.Path) + file.Name;
url = alias.BaseUrl + Utilities.UrlCombine("Content", "Tenants", alias.TenantId.ToString(), "Sites", file.Folder.SiteId.ToString(), file.Folder.Path) + file.Name;
break;
}
return url;

View File

@ -51,7 +51,7 @@ namespace Oqtane.Security
protected virtual bool ShouldValidate(AuthorizationFilterContext context)
{
// ignore antiforgery validation if a bearer token was provided
if (context.HttpContext.Request.Headers.ContainsKey("Authorization"))
if (context.HttpContext.Request.Headers.ContainsKey("Authorization") || context.HttpContext.Request.Headers["User-Agent"] == Constants.MauiUserAgent)
{
return false;
}