Set tenantid to the claims on login.

pull/221/head
Halil İbrahim Kalkan 8 years ago
parent 239cde8cda
commit 946bd7fb79

@ -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);
}
}
}
}

@ -8,7 +8,7 @@ using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Ui;
namespace AbpDesk.Web.Mvc.Controllers
namespace MicroserviceDemo.Web.Controllers
{
/* TODO: This is temporary solution to switch tenant.
*/

@ -1,5 +1,5 @@
@page
@model Volo.Abp.Account.Web.Pages.Account.IdsLoginModel
@model Volo.Abp.Account.Web.Pages.Account.IdentityServerLoginModel
<div class="row">
@if (Model.EnableLocalLogin)

@ -25,7 +25,7 @@ using Volo.Abp.Uow;
namespace Volo.Abp.Account.Web.Pages.Account
{
//TODO: Inherit from LoginModel of Account.Web project. We should design it as extensible.
public class IdsLoginModel : AccountModelBase
public class IdentityServerLoginModel : AccountModelBase
{
[HiddenInput]
[BindProperty(SupportsGet = true)]
@ -55,7 +55,7 @@ namespace Volo.Abp.Account.Web.Pages.Account
private readonly IClientStore _clientStore;
private readonly IEventService _identityServerEvents;
public IdsLoginModel(
public IdentityServerLoginModel(
SignInManager<IdentityUser> signInManager,
IdentityUserManager userManager,
IIdentityServerInteractionService interaction,
@ -83,17 +83,20 @@ namespace Volo.Abp.Account.Web.Pages.Account
{
LoginInput.UserNameOrEmailAddress = context.LoginHint;
//TODO: !!! Always exchanging tenant id, not name!
//TODO: Reference AspNetCore MultiTenancy module and use options to get the tenant key!
var tenant = context.Parameters[TenantResolverConsts.DefaultTenantKey];
if (tenant.IsNullOrEmpty())
{
Response.Cookies.Delete(TenantResolverConsts.DefaultTenantKey);
CurrentTenant.Change(null);
if (Request.Cookies.ContainsKey(TenantResolverConsts.DefaultTenantKey))
{
CurrentTenant.Change(null);
Response.Cookies.Delete(TenantResolverConsts.DefaultTenantKey);
}
}
else
{
Response.Cookies.Append(TenantResolverConsts.DefaultTenantKey, tenant);
CurrentTenant.Change(Guid.Parse(tenant));
Response.Cookies.Append(TenantResolverConsts.DefaultTenantKey, tenant);
}
}

@ -5,8 +5,6 @@ using Volo.Abp.Identity;
namespace Microsoft.Extensions.DependencyInjection
{
//TODO: AspNetUserManager overrides CancellationToken so we can make same functionality available!
public static class AbpIdentityServiceCollectionExtensions
{
public static IdentityBuilder AddAbpIdentity(this IServiceCollection services)
@ -36,9 +34,10 @@ namespace Microsoft.Extensions.DependencyInjection
//AbpRoleStore
services.TryAddScoped<IdentityRoleStore>();
services.TryAddScoped(typeof(IRoleStore<IdentityRole>), provider => provider.GetService(typeof(IdentityRoleStore)));
return services.AddIdentity<IdentityUser, IdentityRole>(setupAction)
.AddDefaultTokenProviders();
.AddDefaultTokenProviders()
.AddClaimsPrincipalFactory<AbpUserClaimsPrincipalFactory>();
//return services.AddIdentityCore<IdentityUser>(setupAction);
}
}

@ -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;
}
}
}

@ -1,21 +1,28 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Services;
using Volo.Abp.Threading;
namespace Volo.Abp.Identity
{
public class IdentityRoleManager : RoleManager<IdentityRole>, IDomainService
{
protected override CancellationToken CancellationToken => _cancellationTokenProvider.Token;
private readonly ICancellationTokenProvider _cancellationTokenProvider;
public IdentityRoleManager(
IdentityRoleStore store,
IEnumerable<IRoleValidator<IdentityRole>> roleValidators,
ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors,
ILogger<IdentityRoleManager> logger)
ILogger<IdentityRoleManager> logger,
ICancellationTokenProvider cancellationTokenProvider)
: base(
store,
roleValidators,
@ -23,6 +30,7 @@ namespace Volo.Abp.Identity
errors,
logger)
{
_cancellationTokenProvider = cancellationTokenProvider;
}
public async Task<IdentityRole> GetByIdAsync(Guid id)

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Identity;
@ -8,11 +9,16 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Services;
using Volo.Abp.Threading;
namespace Volo.Abp.Identity
{
public class IdentityUserManager : UserManager<IdentityUser>, IDomainService
{
protected override CancellationToken CancellationToken => _cancellationTokenProvider.Token;
private readonly ICancellationTokenProvider _cancellationTokenProvider;
public IdentityUserManager(
IdentityUserStore store,
IOptions<IdentityOptions> optionsAccessor,
@ -21,7 +27,8 @@ namespace Volo.Abp.Identity
IEnumerable<IPasswordValidator<IdentityUser>> passwordValidators,
ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors,
IServiceProvider services,
ILogger<IdentityUserManager> logger)
ILogger<IdentityUserManager> logger,
ICancellationTokenProvider cancellationTokenProvider)
: base(
store,
optionsAccessor,
@ -33,7 +40,7 @@ namespace Volo.Abp.Identity
services,
logger)
{
_cancellationTokenProvider = cancellationTokenProvider;
}
public async Task<IdentityUser> GetByIdAsync(Guid id)

@ -42,5 +42,10 @@ namespace Volo.Abp.Security.Claims
/// Default: "phone_number_verified".
/// </summary>
public static string PhoneNumberVerified { get; set; } = "phone_number_verified";
/// <summary>
/// Default: "phone_number_verified".
/// </summary>
public static string TenantId { get; set; } = "tenantid";
}
}

@ -6,7 +6,9 @@ namespace Volo.Abp.Threading
{
public static CancellationToken FallbackToProvider(this ICancellationTokenProvider provider, CancellationToken prefferedValue = default)
{
return prefferedValue == default ? provider.Token : prefferedValue;
return prefferedValue == default || prefferedValue == CancellationToken.None
? provider.Token
: prefferedValue;
}
}
}
Loading…
Cancel
Save