mirror of https://github.com/abpframework/abp.git
10 changed files with 144 additions and 16 deletions
@ -0,0 +1,66 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.AspNetCore.MultiTenancy; |
||||
|
using Volo.Abp.AspNetCore.Mvc; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.Ui; |
||||
|
|
||||
|
namespace MicroserviceDemo.AuthServer.Controllers |
||||
|
{ |
||||
|
/* TODO: This is temporary solution to switch tenant. |
||||
|
*/ |
||||
|
|
||||
|
public class MultiTenancyController : AbpController |
||||
|
{ |
||||
|
private readonly ITenantStore _tenantStore; |
||||
|
private readonly AspNetCoreMultiTenancyOptions _options; |
||||
|
|
||||
|
public MultiTenancyController(ITenantStore tenantStore, IOptions<AspNetCoreMultiTenancyOptions> options) |
||||
|
{ |
||||
|
_tenantStore = tenantStore; |
||||
|
_options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public async Task<ActionResult> SwitchTenant(string tenant = "") |
||||
|
{ |
||||
|
if (tenant.IsNullOrEmpty()) |
||||
|
{ |
||||
|
HttpContext.Response.Cookies.Delete(_options.TenantKey); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
var tenantInfo = await FindTenantAsync(tenant); |
||||
|
if (tenantInfo == null) |
||||
|
{ |
||||
|
throw new UserFriendlyException("Unknown tenant: " + tenant); |
||||
|
} |
||||
|
|
||||
|
HttpContext.Response.Cookies.Append( |
||||
|
_options.TenantKey, |
||||
|
tenantInfo.Id.ToString(), |
||||
|
new CookieOptions |
||||
|
{ |
||||
|
Expires = DateTimeOffset.Now.AddYears(1) |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
return Redirect("/"); |
||||
|
} |
||||
|
|
||||
|
private async Task<TenantInfo> FindTenantAsync(string tenantIdOrName) |
||||
|
{ |
||||
|
if (Guid.TryParse(tenantIdOrName, out var parsedTenantId)) |
||||
|
{ |
||||
|
return await _tenantStore.FindAsync(parsedTenantId); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
return await _tenantStore.FindAsync(tenantIdOrName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Security.Claims; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Identity; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
using Volo.Abp.Uow; |
||||
|
|
||||
|
namespace Volo.Abp.Identity |
||||
|
{ |
||||
|
public class AbpUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<IdentityUser, IdentityRole>, ITransientDependency |
||||
|
{ |
||||
|
public AbpUserClaimsPrincipalFactory( |
||||
|
UserManager<IdentityUser> userManager, |
||||
|
RoleManager<IdentityRole> roleManager, |
||||
|
IOptions<IdentityOptions> options) |
||||
|
: base( |
||||
|
userManager, |
||||
|
roleManager, |
||||
|
options) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
[UnitOfWork] |
||||
|
public override async Task<ClaimsPrincipal> CreateAsync(IdentityUser user) |
||||
|
{ |
||||
|
var principal = await base.CreateAsync(user); |
||||
|
|
||||
|
if (user.TenantId.HasValue) |
||||
|
{ |
||||
|
principal.Identities.First().AddClaim(new Claim(AbpClaimTypes.TenantId, user.TenantId.ToString())); |
||||
|
} |
||||
|
|
||||
|
return principal; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue