Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created April 7, 2021 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/7f60d67f1c03fdb3e48a103e968ff23a to your computer and use it in GitHub Desktop.
Save danielplawgo/7f60d67f1c03fdb3e48a103e968ff23a to your computer and use it in GitHub Desktop.
Multi tenant - określenie tenanta
public class ClaimResolutionStrategy : ITenantResolutionStrategy
{
private readonly IHttpContextAccessor _httpContextAccessor;
public ClaimResolutionStrategy(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public Task<Guid> GetTenantIdentifierAsync()
{
return Task.FromResult(GetTenantIdentifier());
}
public Guid GetTenantIdentifier()
{
if (_httpContextAccessor.HttpContext?.User?.Identity?.IsAuthenticated == true)
{
return _httpContextAccessor.HttpContext.User.Identity.GetTenant();
}
return Guid.Empty;
}
}
public static class IIdentityExtensions
{
public static Guid GetTenant(this IIdentity identity)
{
var claimIdentity = identity as ClaimsIdentity;
var userTenant = claimIdentity?.FindFirst("tenant")?.Value;
if (userTenant == null)
{
return Guid.Empty;
}
return Guid.Parse(userTenant);
}
}
public interface ITenantResolutionStrategy
{
Task<string> GetTenantIdentifierAsync();
string GetTenantIdentifier();
}
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl ??= Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(Input.Email);
if (user != null)
{
if (await _userManager.CheckPasswordAsync(user, Input.Password))
{
var claims = new List<Claim>();
var tenant = user.Tenants.FirstOrDefault();
if (tenant != null)
{
claims.Add(new Claim("tenant", tenant.Id.ToString()));
}
await _signInManager.SignInWithClaimsAsync(user, true, claims);
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
}
...
}
public class PathResolutionStrategy : ITenantResolutionStrategy
{
private readonly IHttpContextAccessor _httpContextAccessor;
public PathResolutionStrategy(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public Task<string> GetTenantIdentifierAsync()
{
return Task.FromResult(GetTenantIdentifier());
}
public string GetTenantIdentifier()
{
if (_httpContextAccessor.HttpContext?.Request.RouteValues.ContainsKey("tenant") == false)
{
return null;
}
return _httpContextAccessor.HttpContext?.Request.RouteValues["tenant"]?.ToString();
}
}
public class Startup
{
....
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
....
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "home",
pattern: "/",
defaults: new { controller = "Home", action = "Index" });
endpoints.MapControllerRoute(
name: "default",
pattern: "{tenant}/{controller=Tenants}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment