Created
April 7, 2021 03:49
-
-
Save danielplawgo/7f60d67f1c03fdb3e48a103e968ff23a to your computer and use it in GitHub Desktop.
Multi tenant - określenie tenanta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ITenantResolutionStrategy | |
{ | |
Task<string> GetTenantIdentifierAsync(); | |
string GetTenantIdentifier(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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