Multi-tenant role authorization
This commit is contained in:
@ -51,20 +51,20 @@
|
||||
|
||||
private async Task Login()
|
||||
{
|
||||
User user = new User();
|
||||
user.SiteId = PageState.Site.SiteId;
|
||||
user.Username = Username;
|
||||
user.Password = Password;
|
||||
user.IsPersistent = Remember;
|
||||
user = await UserService.LoginUserAsync(user);
|
||||
if (user.IsAuthenticated)
|
||||
{
|
||||
string ReturnUrl = PageState.QueryString["returnurl"];
|
||||
string ReturnUrl = PageState.QueryString["returnurl"];
|
||||
|
||||
var authstateprovider = (IdentityAuthenticationStateProvider)ServiceProvider.GetService(typeof(IdentityAuthenticationStateProvider));
|
||||
if (authstateprovider == null)
|
||||
var authstateprovider = (IdentityAuthenticationStateProvider)ServiceProvider.GetService(typeof(IdentityAuthenticationStateProvider));
|
||||
if (authstateprovider == null)
|
||||
{
|
||||
// server-side Blazor
|
||||
User user = new User();
|
||||
user.SiteId = PageState.Site.SiteId;
|
||||
user.Username = Username;
|
||||
user.Password = Password;
|
||||
user = await UserService.LoginUserAsync(user, false, false);
|
||||
if (user.IsAuthenticated)
|
||||
{
|
||||
// server-side Blazor
|
||||
// complete the login on the server so that the cookies are set correctly on SignalR
|
||||
var interop = new Interop(jsRuntime);
|
||||
string antiforgerytoken = await interop.GetElementByName("__RequestVerificationToken");
|
||||
var fields = new { __RequestVerificationToken = antiforgerytoken, username = Username, password = Password, remember = Remember, returnurl = ReturnUrl };
|
||||
@ -72,15 +72,27 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
// client-side Blazor
|
||||
authstateprovider.NotifyAuthenticationChanged();
|
||||
PageState.Reload = Constants.ReloadPage;
|
||||
UriHelper.NavigateTo(NavigateUrl(ReturnUrl));
|
||||
Message = "<div class=\"alert alert-danger\" role=\"alert\">Login Failed. Please Remember That Passwords Are Case Sensitive.</div>";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Message = "<div class=\"alert alert-danger\" role=\"alert\">Login Failed. Please Remember That Passwords Are Case Sensitive.</div>";
|
||||
// client-side Blazor
|
||||
User user = new User();
|
||||
user.SiteId = PageState.Site.SiteId;
|
||||
user.Username = Username;
|
||||
user.Password = Password;
|
||||
user = await UserService.LoginUserAsync(user, true, Remember);
|
||||
if (user.IsAuthenticated)
|
||||
{
|
||||
authstateprovider.NotifyAuthenticationChanged();
|
||||
PageState.Reload = Constants.ReloadSite;
|
||||
UriHelper.NavigateTo(NavigateUrl(ReturnUrl));
|
||||
}
|
||||
else
|
||||
{
|
||||
Message = "<div class=\"alert alert-danger\" role=\"alert\">Login Failed. Please Remember That Passwords Are Case Sensitive.</div>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
@inject IPageService PageService
|
||||
@inject IThemeService ThemeService
|
||||
|
||||
@message
|
||||
@((MarkupString)message)
|
||||
|
||||
<table class="form-group">
|
||||
<tr>
|
||||
@ -144,7 +144,7 @@
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = ex.Message;
|
||||
message = "<div class=\"alert alert-danger\" role=\"alert\">" + ex.Message + "</div><br /><br />";
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,11 +185,18 @@
|
||||
await PageService.AddPageAsync(page);
|
||||
|
||||
PageState.Reload = Constants.ReloadSite;
|
||||
UriHelper.NavigateTo(NavigateUrl(path));
|
||||
if (PageState.Page.Name == "Page Management")
|
||||
{
|
||||
UriHelper.NavigateTo(NavigateUrl());
|
||||
}
|
||||
else
|
||||
{
|
||||
UriHelper.NavigateTo(NavigateUrl(path));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = ex.Message;
|
||||
message = "<div class=\"alert alert-danger\" role=\"alert\">" + ex.Message + "</div><br /><br />";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
@inject IPageService PageService
|
||||
@inject IThemeService ThemeService
|
||||
|
||||
@message
|
||||
@((MarkupString)message)
|
||||
|
||||
<table class="form-group">
|
||||
<tr>
|
||||
@ -116,7 +116,8 @@
|
||||
</table>
|
||||
<button type="button" class="btn btn-danger" @onclick="@DeletePage">Delete</button>
|
||||
<NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>
|
||||
<br /><br />
|
||||
<br />
|
||||
<br />
|
||||
<AuditInfo CreatedBy="@createdby" CreatedOn="@createdon" ModifiedBy="@modifiedby" ModifiedOn="@modifiedon"></AuditInfo>
|
||||
|
||||
@code {
|
||||
@ -172,7 +173,7 @@
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = ex.Message;
|
||||
message = "<div class=\"alert alert-danger\" role=\"alert\">" + ex.Message + "</div><br /><br />";
|
||||
}
|
||||
}
|
||||
|
||||
@ -182,11 +183,18 @@
|
||||
{
|
||||
await PageService.DeletePageAsync(Int32.Parse(PageState.QueryString["id"]));
|
||||
PageState.Reload = Constants.ReloadSite;
|
||||
UriHelper.NavigateTo(NavigateUrl());
|
||||
if (PageState.Page.Name == "Page Management")
|
||||
{
|
||||
UriHelper.NavigateTo(NavigateUrl());
|
||||
}
|
||||
else
|
||||
{
|
||||
UriHelper.NavigateTo(NavigateUrl(""));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = ex.Message;
|
||||
message = "<div class=\"alert alert-danger\" role=\"alert\">" + ex.Message + "</div><br /><br />";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
@inject IPageService PageService
|
||||
@inject IThemeService ThemeService
|
||||
|
||||
@message
|
||||
@((MarkupString)message)
|
||||
|
||||
<table class="form-group">
|
||||
<tr>
|
||||
@ -116,7 +116,8 @@
|
||||
</table>
|
||||
<button type="button" class="btn btn-success" @onclick="@SavePage">Save</button>
|
||||
<NavLink class="btn btn-secondary" href="@NavigateUrl()">Cancel</NavLink>
|
||||
<br /><br />
|
||||
<br />
|
||||
<br />
|
||||
<AuditInfo CreatedBy="@createdby" CreatedOn="@createdon" ModifiedBy="@modifiedby" ModifiedOn="@modifiedon"></AuditInfo>
|
||||
|
||||
@code {
|
||||
@ -179,7 +180,7 @@
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = ex.Message;
|
||||
message = "<div class=\"alert alert-danger\" role=\"alert\">" + ex.Message + "</div><br /><br />";
|
||||
}
|
||||
}
|
||||
|
||||
@ -220,11 +221,18 @@
|
||||
await PageService.UpdatePageAsync(page);
|
||||
|
||||
PageState.Reload = Constants.ReloadSite;
|
||||
UriHelper.NavigateTo(NavigateUrl(path));
|
||||
if (PageState.Page.Name == "Page Management")
|
||||
{
|
||||
UriHelper.NavigateTo(NavigateUrl());
|
||||
}
|
||||
else
|
||||
{
|
||||
UriHelper.NavigateTo(NavigateUrl(path));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = ex.Message;
|
||||
message = "<div class=\"alert alert-danger\" role=\"alert\">" + ex.Message + "</div><br /><br />";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@
|
||||
public string Style { get; set; }
|
||||
|
||||
string text = "";
|
||||
string style = "";
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
|
@ -4,29 +4,39 @@ using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Services;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Providers
|
||||
{
|
||||
public class IdentityAuthenticationStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private readonly IUriHelper urihelper;
|
||||
private readonly SiteState sitestate;
|
||||
|
||||
public IdentityAuthenticationStateProvider(IUriHelper urihelper)
|
||||
public IdentityAuthenticationStateProvider(IUriHelper urihelper, SiteState sitestate)
|
||||
{
|
||||
this.urihelper = urihelper;
|
||||
this.sitestate = sitestate;
|
||||
}
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
// hack: create a new HttpClient rather than relying on the registered service as the AuthenticationStateProvider is initialized prior to IUriHelper ( https://github.com/aspnet/AspNetCore/issues/11867 )
|
||||
HttpClient http = new HttpClient();
|
||||
Uri uri = new Uri(urihelper.GetAbsoluteUri());
|
||||
string apiurl = uri.Scheme + "://" + uri.Authority + "/~/api/User/authenticate";
|
||||
string apiurl = ServiceBase.CreateApiUrl(sitestate.Alias, urihelper.GetAbsoluteUri(), "User") + "/authenticate";
|
||||
User user = await http.GetJsonAsync<User>(apiurl);
|
||||
|
||||
var identity = user.IsAuthenticated
|
||||
? new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, user.Username) }, "Identity.Application")
|
||||
: new ClaimsIdentity();
|
||||
ClaimsIdentity identity = new ClaimsIdentity();
|
||||
if (user.IsAuthenticated)
|
||||
{
|
||||
identity = new ClaimsIdentity("Identity.Application");
|
||||
identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
|
||||
foreach(string role in user.Roles.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
identity.AddClaim(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
}
|
||||
return new AuthenticationState(new ClaimsPrincipal(identity));
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ namespace Oqtane.Services
|
||||
|
||||
Task DeleteUserAsync(int UserId);
|
||||
|
||||
Task<User> LoginUserAsync(User User);
|
||||
Task<User> LoginUserAsync(User User, bool SetCookie, bool IsPersistent);
|
||||
|
||||
Task LogoutUserAsync();
|
||||
|
||||
|
@ -8,7 +8,7 @@ namespace Oqtane.Services
|
||||
public class ServiceBase
|
||||
{
|
||||
|
||||
public string CreateApiUrl(Alias alias, string absoluteUri, string serviceName)
|
||||
public static string CreateApiUrl(Alias alias, string absoluteUri, string serviceName)
|
||||
{
|
||||
string apiurl = "";
|
||||
if (alias != null)
|
||||
|
@ -57,9 +57,9 @@ namespace Oqtane.Services
|
||||
await http.DeleteAsync(apiurl + "/" + UserId.ToString());
|
||||
}
|
||||
|
||||
public async Task<User> LoginUserAsync(User User)
|
||||
public async Task<User> LoginUserAsync(User User, bool SetCookie, bool IsPersistent)
|
||||
{
|
||||
return await http.PostJsonAsync<User>(apiurl + "/login", User);
|
||||
return await http.PostJsonAsync<User>(apiurl + "/login?setcookie=" + SetCookie.ToString() + "&persistent =" + IsPersistent.ToString(), User);
|
||||
}
|
||||
|
||||
public async Task LogoutUserAsync()
|
||||
|
@ -13,10 +13,14 @@ namespace Oqtane.Shared
|
||||
{
|
||||
url += alias + "/";
|
||||
}
|
||||
if (path != "")
|
||||
if (path != "" && path != "/")
|
||||
{
|
||||
url += path + "/";
|
||||
}
|
||||
if (url.EndsWith("/"))
|
||||
{
|
||||
url = url.Substring(0, url.Length - 1);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(parameters))
|
||||
{
|
||||
url += "?" + parameters;
|
||||
@ -31,10 +35,6 @@ namespace Oqtane.Shared
|
||||
public static string EditUrl(string alias, string path, int moduleid, string action, string parameters)
|
||||
{
|
||||
string url = NavigateUrl(alias, path, "");
|
||||
if ( url == "/" )
|
||||
{
|
||||
url = "";
|
||||
}
|
||||
if (moduleid != -1)
|
||||
{
|
||||
url += "/" + moduleid.ToString();
|
||||
|
@ -51,7 +51,7 @@
|
||||
// client-side Blazor
|
||||
authstateprovider.NotifyAuthenticationChanged();
|
||||
PageState.Reload = Constants.ReloadSite;
|
||||
UriHelper.NavigateTo(NavigateUrl(PageState.Page.Path));
|
||||
UriHelper.NavigateTo(NavigateUrl(PageState.Page.Path, "logout"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user