2019-05-17 11:58:09 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-09-16 09:21:10 +00:00
|
|
|
using System.Linq;
|
2019-05-17 11:58:09 +00:00
|
|
|
using System.Security;
|
|
|
|
using System.Security.Authentication;
|
2019-09-16 09:21:10 +00:00
|
|
|
using System.Security.Claims;
|
2019-05-17 11:58:09 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Web;
|
2020-02-03 10:41:24 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Common;
|
2019-05-17 11:58:09 +00:00
|
|
|
using ASC.Common.Logging;
|
|
|
|
using ASC.Common.Security;
|
|
|
|
using ASC.Common.Security.Authentication;
|
|
|
|
using ASC.Common.Security.Authorizing;
|
|
|
|
using ASC.Core.Billing;
|
2019-05-22 09:28:34 +00:00
|
|
|
using ASC.Core.Common.Security;
|
2019-05-17 11:58:09 +00:00
|
|
|
using ASC.Core.Security.Authentication;
|
2019-10-22 16:08:37 +00:00
|
|
|
using ASC.Core.Security.Authorizing;
|
2019-05-17 11:58:09 +00:00
|
|
|
using ASC.Core.Tenants;
|
|
|
|
using ASC.Core.Users;
|
2020-02-03 10:41:24 +00:00
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-10-17 15:55:35 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2019-05-17 11:58:09 +00:00
|
|
|
|
|
|
|
namespace ASC.Core
|
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
public class SecurityContext
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-10-17 15:55:35 +00:00
|
|
|
private readonly ILog log;
|
2019-05-17 11:58:09 +00:00
|
|
|
|
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
public IAccount CurrentAccount
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
get => AuthContext.CurrentAccount;
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
public bool IsAuthenticated
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
get => AuthContext.IsAuthenticated;
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 11:28:30 +00:00
|
|
|
private UserManager UserManager { get; }
|
|
|
|
private AuthManager Authentication { get; }
|
|
|
|
private AuthContext AuthContext { get; }
|
|
|
|
private TenantManager TenantManager { get; }
|
|
|
|
private UserFormatter UserFormatter { get; }
|
|
|
|
private CookieStorage CookieStorage { get; }
|
2020-08-12 09:58:08 +00:00
|
|
|
private TenantCookieSettingsHelper TenantCookieSettingsHelper { get; }
|
2019-10-31 11:28:30 +00:00
|
|
|
private IHttpContextAccessor HttpContextAccessor { get; }
|
2019-09-09 12:56:33 +00:00
|
|
|
|
|
|
|
public SecurityContext(
|
|
|
|
UserManager userManager,
|
|
|
|
AuthManager authentication,
|
2019-09-11 10:42:52 +00:00
|
|
|
AuthContext authContext,
|
2019-09-24 10:32:12 +00:00
|
|
|
TenantManager tenantManager,
|
2019-10-09 15:04:46 +00:00
|
|
|
UserFormatter userFormatter,
|
2019-10-18 08:48:27 +00:00
|
|
|
CookieStorage cookieStorage,
|
2019-11-08 12:21:51 +00:00
|
|
|
TenantCookieSettingsHelper tenantCookieSettingsHelper,
|
2019-11-06 15:03:09 +00:00
|
|
|
IOptionsMonitor<ILog> options
|
2019-09-09 12:56:33 +00:00
|
|
|
)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-11-06 15:03:09 +00:00
|
|
|
log = options.CurrentValue;
|
2019-09-09 12:56:33 +00:00
|
|
|
UserManager = userManager;
|
|
|
|
Authentication = authentication;
|
|
|
|
AuthContext = authContext;
|
2019-09-17 08:07:46 +00:00
|
|
|
TenantManager = tenantManager;
|
2019-09-24 10:32:12 +00:00
|
|
|
UserFormatter = userFormatter;
|
2019-10-18 08:48:27 +00:00
|
|
|
CookieStorage = cookieStorage;
|
2019-11-08 12:21:51 +00:00
|
|
|
TenantCookieSettingsHelper = tenantCookieSettingsHelper;
|
2020-04-28 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public SecurityContext(
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
UserManager userManager,
|
|
|
|
AuthManager authentication,
|
|
|
|
AuthContext authContext,
|
|
|
|
TenantManager tenantManager,
|
|
|
|
UserFormatter userFormatter,
|
|
|
|
CookieStorage cookieStorage,
|
|
|
|
TenantCookieSettingsHelper tenantCookieSettingsHelper,
|
|
|
|
IOptionsMonitor<ILog> options
|
|
|
|
) : this(userManager, authentication, authContext, tenantManager, userFormatter, cookieStorage, tenantCookieSettingsHelper, options)
|
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
HttpContextAccessor = httpContextAccessor;
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-15 13:11:05 +00:00
|
|
|
public string AuthenticateMe(string login, string passwordHash)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
|
|
|
if (login == null) throw new ArgumentNullException("login");
|
2020-09-15 13:11:05 +00:00
|
|
|
if (passwordHash == null) throw new ArgumentNullException("passwordHash");
|
2019-05-17 11:58:09 +00:00
|
|
|
|
2019-09-17 08:07:46 +00:00
|
|
|
var tenantid = TenantManager.GetCurrentTenant().TenantId;
|
2020-09-15 13:11:05 +00:00
|
|
|
var u = UserManager.GetUsersByPasswordHash(tenantid, login, passwordHash);
|
2019-05-17 11:58:09 +00:00
|
|
|
|
2019-09-24 10:32:12 +00:00
|
|
|
return AuthenticateMe(new UserAccount(u, tenantid, UserFormatter));
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
public bool AuthenticateMe(string cookie)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(cookie))
|
|
|
|
{
|
|
|
|
|
|
|
|
if (cookie.Equals("Bearer", StringComparison.InvariantCulture))
|
|
|
|
{
|
|
|
|
var ipFrom = string.Empty;
|
|
|
|
var address = string.Empty;
|
2019-09-09 12:56:33 +00:00
|
|
|
if (HttpContextAccessor?.HttpContext != null)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
var request = HttpContextAccessor?.HttpContext.Request;
|
2019-05-17 11:58:09 +00:00
|
|
|
ipFrom = "from " + (request.Headers["X-Forwarded-For"].ToString() ?? request.GetUserHostAddress());
|
|
|
|
address = "for " + request.GetUrlRewriter();
|
|
|
|
}
|
|
|
|
log.InfoFormat("Empty Bearer cookie: {0} {1}", ipFrom, address);
|
|
|
|
}
|
2020-09-15 13:11:05 +00:00
|
|
|
else if (CookieStorage.DecryptCookie(cookie, out var tenant, out var userid, out var indexTenant, out var expire, out var indexUser))
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-17 08:07:46 +00:00
|
|
|
if (tenant != TenantManager.GetCurrentTenant().TenantId)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:21:51 +00:00
|
|
|
var settingsTenant = TenantCookieSettingsHelper.GetForTenant(tenant);
|
2019-05-17 11:58:09 +00:00
|
|
|
if (indexTenant != settingsTenant.Index)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expire != DateTime.MaxValue && expire < DateTime.UtcNow)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-09-15 13:11:05 +00:00
|
|
|
var settingsUser = TenantCookieSettingsHelper.GetForUser(userid);
|
|
|
|
if (indexUser != settingsUser.Index)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2020-09-15 13:11:05 +00:00
|
|
|
return false;
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
2020-09-15 13:11:05 +00:00
|
|
|
|
2020-09-30 11:50:39 +00:00
|
|
|
_ = AuthenticateMe(new UserAccount(new UserInfo { ID = userid }, tenant, UserFormatter));
|
2020-09-15 13:11:05 +00:00
|
|
|
|
2019-05-17 11:58:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (InvalidCredentialException ice)
|
|
|
|
{
|
2020-09-15 13:11:05 +00:00
|
|
|
log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}", ice.Message, cookie, tenant, userid);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
catch (SecurityException se)
|
|
|
|
{
|
2020-09-15 13:11:05 +00:00
|
|
|
log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}", se.Message, cookie, tenant, userid);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception err)
|
|
|
|
{
|
2020-09-15 13:11:05 +00:00
|
|
|
log.ErrorFormat("Authenticate error: cookie {0}, tenant {1}, userid {2}, : {3}", cookie, tenant, userid, err);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var ipFrom = string.Empty;
|
|
|
|
var address = string.Empty;
|
2019-09-09 12:56:33 +00:00
|
|
|
if (HttpContextAccessor?.HttpContext != null)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
var request = HttpContextAccessor?.HttpContext.Request;
|
2019-05-17 11:58:09 +00:00
|
|
|
address = "for " + request.GetUrlRewriter();
|
|
|
|
ipFrom = "from " + (request.Headers["X-Forwarded-For"].ToString() ?? request.GetUserHostAddress());
|
|
|
|
}
|
|
|
|
log.WarnFormat("Can not decrypt cookie: {0} {1} {2}", cookie, ipFrom, address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:27:36 +00:00
|
|
|
public string AuthenticateMe(IAccount account, List<Claim> additionalClaims = null)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
|
|
|
if (account == null || account.Equals(Configuration.Constants.Guest)) throw new InvalidCredentialException("account");
|
|
|
|
|
|
|
|
var roles = new List<string> { Role.Everyone };
|
|
|
|
string cookie = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (account is ISystemAccount && account.ID == Configuration.Constants.CoreSystem.ID)
|
|
|
|
{
|
|
|
|
roles.Add(Role.System);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account is IUserAccount)
|
|
|
|
{
|
2019-09-17 08:07:46 +00:00
|
|
|
var tenant = TenantManager.GetCurrentTenant();
|
2019-08-09 12:28:19 +00:00
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
var u = UserManager.GetUsers(account.ID);
|
2019-05-17 11:58:09 +00:00
|
|
|
|
|
|
|
if (u.ID == Users.Constants.LostUser.ID)
|
|
|
|
{
|
|
|
|
throw new InvalidCredentialException("Invalid username or password.");
|
|
|
|
}
|
|
|
|
if (u.Status != EmployeeStatus.Active)
|
|
|
|
{
|
|
|
|
throw new SecurityException("Account disabled.");
|
|
|
|
}
|
2019-08-09 12:28:19 +00:00
|
|
|
|
2019-05-17 11:58:09 +00:00
|
|
|
// for LDAP users only
|
|
|
|
if (u.Sid != null)
|
|
|
|
{
|
2019-09-17 08:07:46 +00:00
|
|
|
if (!TenantManager.GetTenantQuota(tenant.TenantId).Ldap)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
|
|
|
throw new BillingException("Your tariff plan does not support this option.", "Ldap");
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 11:34:58 +00:00
|
|
|
if (UserManager.IsUserInGroup(u.ID, Users.Constants.GroupAdmin.ID))
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
|
|
|
roles.Add(Role.Administrators);
|
|
|
|
}
|
|
|
|
roles.Add(Role.Users);
|
|
|
|
|
2019-09-24 10:32:12 +00:00
|
|
|
account = new UserAccount(u, TenantManager.GetCurrentTenant().TenantId, UserFormatter);
|
2019-10-18 08:48:27 +00:00
|
|
|
cookie = CookieStorage.EncryptCookie(TenantManager.GetCurrentTenant().TenantId, account.ID);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 09:21:10 +00:00
|
|
|
var claims = new List<Claim>
|
|
|
|
{
|
|
|
|
new Claim(ClaimTypes.Sid, account.ID.ToString()),
|
|
|
|
new Claim(ClaimTypes.Name, account.Name)
|
|
|
|
};
|
|
|
|
claims.AddRange(roles.Select(r => new Claim(ClaimTypes.Role, r)));
|
|
|
|
|
2019-09-27 09:18:48 +00:00
|
|
|
if (additionalClaims != null)
|
2019-09-23 15:36:22 +00:00
|
|
|
{
|
|
|
|
claims.AddRange(additionalClaims);
|
|
|
|
}
|
2019-09-16 12:35:44 +00:00
|
|
|
AuthContext.Principal = new CustomClaimsPrincipal(new ClaimsIdentity(account, claims), account);
|
2019-05-17 11:58:09 +00:00
|
|
|
|
|
|
|
return cookie;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:45:37 +00:00
|
|
|
public string AuthenticateMe(Guid userId, List<Claim> additionalClaims = null)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-27 10:45:37 +00:00
|
|
|
return AuthenticateMe(Authentication.GetAccountByID(TenantManager.GetCurrentTenant().TenantId, userId), additionalClaims);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
public void Logout()
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2020-02-03 10:41:24 +00:00
|
|
|
AuthContext.Logout();
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 13:11:05 +00:00
|
|
|
public void SetUserPasswordHash(Guid userID, string passwordHash)
|
|
|
|
{
|
|
|
|
var tenantid = TenantManager.GetCurrentTenant().TenantId;
|
|
|
|
var u = UserManager.GetUsersByPasswordHash(tenantid, userID.ToString(), passwordHash);
|
|
|
|
if (!Equals(u, Users.Constants.LostUser))
|
|
|
|
{
|
|
|
|
throw new PasswordException("A new password must be used");
|
|
|
|
}
|
|
|
|
|
|
|
|
Authentication.SetUserPasswordHash(userID, passwordHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class PasswordException : Exception
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2020-09-15 13:11:05 +00:00
|
|
|
public PasswordException(string message) : base(message)
|
|
|
|
{
|
|
|
|
}
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
2019-09-09 12:56:33 +00:00
|
|
|
}
|
2019-05-17 11:58:09 +00:00
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
public class PermissionContext
|
|
|
|
{
|
2020-08-12 09:58:08 +00:00
|
|
|
public IPermissionResolver PermissionResolver { get; set; }
|
|
|
|
private AuthContext AuthContext { get; }
|
2019-05-17 11:58:09 +00:00
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
public PermissionContext(IPermissionResolver permissionResolver, AuthContext authContext)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
PermissionResolver = permissionResolver;
|
|
|
|
AuthContext = authContext;
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public bool CheckPermissions(params IAction[] actions)
|
2019-09-09 12:56:33 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
return PermissionResolver.Check(AuthContext.CurrentAccount, actions);
|
2019-09-09 12:56:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public bool CheckPermissions(ISecurityObject securityObject, params IAction[] actions)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
return CheckPermissions(securityObject, null, actions);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public bool CheckPermissions(ISecurityObjectId objectId, ISecurityObjectProvider securityObjProvider, params IAction[] actions)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
return PermissionResolver.Check(AuthContext.CurrentAccount, objectId, securityObjProvider, actions);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public void DemandPermissions(params IAction[] actions)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
PermissionResolver.Demand(AuthContext.CurrentAccount, actions);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public void DemandPermissions(ISecurityObject securityObject, params IAction[] actions)
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
DemandPermissions(securityObject, null, actions);
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public void DemandPermissions(ISecurityObjectId objectId, ISecurityObjectProvider securityObjProvider, params IAction[] actions)
|
2019-09-09 12:56:33 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
PermissionResolver.Demand(AuthContext.CurrentAccount, objectId, securityObjProvider, actions);
|
2019-09-09 12:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class AuthContext
|
|
|
|
{
|
2019-10-22 16:08:37 +00:00
|
|
|
private IHttpContextAccessor HttpContextAccessor { get; }
|
|
|
|
|
2020-02-27 08:44:55 +00:00
|
|
|
public AuthContext()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-09 12:56:33 +00:00
|
|
|
public AuthContext(IHttpContextAccessor httpContextAccessor)
|
|
|
|
{
|
|
|
|
HttpContextAccessor = httpContextAccessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IAccount CurrentAccount
|
2019-05-17 11:58:09 +00:00
|
|
|
{
|
2019-09-09 12:56:33 +00:00
|
|
|
get { return Principal?.Identity is IAccount ? (IAccount)Principal.Identity : Configuration.Constants.Guest; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsAuthenticated
|
|
|
|
{
|
|
|
|
get { return CurrentAccount.IsAuthenticated; }
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:41:24 +00:00
|
|
|
public void Logout()
|
|
|
|
{
|
|
|
|
Principal = null;
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:35:44 +00:00
|
|
|
internal ClaimsPrincipal Principal
|
2019-05-17 08:32:23 +00:00
|
|
|
{
|
2019-09-16 12:35:44 +00:00
|
|
|
get => Thread.CurrentPrincipal as ClaimsPrincipal ?? HttpContextAccessor?.HttpContext?.User;
|
2019-05-17 11:58:09 +00:00
|
|
|
set
|
|
|
|
{
|
2019-09-16 09:21:10 +00:00
|
|
|
Thread.CurrentPrincipal = value;
|
2019-09-16 12:35:44 +00:00
|
|
|
if (HttpContextAccessor?.HttpContext != null) HttpContextAccessor.HttpContext.User = value;
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
2019-05-17 08:32:23 +00:00
|
|
|
}
|
2019-05-17 11:58:09 +00:00
|
|
|
}
|
2019-10-22 16:08:37 +00:00
|
|
|
|
2019-11-11 09:03:49 +00:00
|
|
|
public static class AuthContextConfigExtension
|
2019-10-22 16:08:37 +00:00
|
|
|
{
|
2020-02-17 08:58:14 +00:00
|
|
|
public static DIHelper AddSecurityContextService(this DIHelper services)
|
2019-10-31 11:28:30 +00:00
|
|
|
{
|
2020-07-17 10:52:28 +00:00
|
|
|
if (services.TryAddScoped<SecurityContext>())
|
|
|
|
{
|
|
|
|
return services
|
|
|
|
.AddCookieStorageService()
|
|
|
|
.AddTenantCookieSettingsService()
|
|
|
|
.AddAuthManager()
|
|
|
|
.AddUserFormatter()
|
|
|
|
.AddAuthContextService()
|
|
|
|
.AddUserManagerService()
|
|
|
|
.AddTenantManagerService();
|
|
|
|
}
|
|
|
|
|
|
|
|
return services;
|
2019-10-31 11:28:30 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
public static DIHelper AddAuthContextService(this DIHelper services)
|
2019-10-22 16:08:37 +00:00
|
|
|
{
|
2020-09-30 11:50:39 +00:00
|
|
|
_ = services.TryAddScoped<AuthContext>();
|
2020-02-17 08:58:14 +00:00
|
|
|
return services;
|
2019-10-22 16:08:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
public static DIHelper AddPermissionContextService(this DIHelper services)
|
2019-10-22 16:08:37 +00:00
|
|
|
{
|
2020-07-17 10:52:28 +00:00
|
|
|
if (services.TryAddScoped<PermissionContext>())
|
|
|
|
{
|
|
|
|
return services
|
|
|
|
.AddAuthContextService()
|
|
|
|
.AddPermissionResolverService();
|
|
|
|
}
|
2019-10-31 11:28:30 +00:00
|
|
|
|
2020-07-17 10:52:28 +00:00
|
|
|
return services;
|
2019-10-22 16:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|