Hi,
How can I detect the current user/ tenant from the login page without using the tenant selection or subdomain?
- ASP.NET
- MVC
- .NET CORE
- Latest ABP Framework according to Zero 9.1
28 Answer(s)
-
0
Hi alfar_re, At present, if you want to select tenants without perception, the best way is to distinguish by subdomain. Because the user cannot know which tenant the current user belongs to when logging in for the first time. Perhaps you can add a unique prefix to the username when creating a user, rewrite a tenant resolver, and select tenants based on the prefix.
-
0
Hi @zony,
I see. The good thing is that the system we are building only allows one user per tenant and the username is a mobile number which is unique. That means that you will only have one tenant with one mobile number. That way it'll be easy to identify the tenants.
-
0
Hi @alfar_re
You can also query AbpUserAccounts table if you don't want to use subdomains.
-
1
Hi @ismcagdas,
I see. I will try this out and let you know.
-
0
Hi @alfar_re
Just don't forget to switch to HOST side while querying this table, see https://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy#switching-between-host-and-tenants
-
0
Hi,
Gone through the login logic. Not sure I fully understand how it functions but I modified this line at line 180 > AccountsController
var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, await GetTenancyNameFromUsernameAsync(loginModel.UsernameOrEmailAddress));
Then reused the code like below;
`private async Task
if (isTenantAvailable != null) { return isTenantAvailable.TenantId.ToString(); } else { throw new InvalidOperationException($"Given user ({userName}) could not be found!"); } }`
Let me know if this is the right direction or point me to the right direction. Thanks.
-
0
Hi,
Still haven't gotten a response on this. I made some changes and it worked for the tenant and I'd like to know how or where I switch to host.
`var usr = await _accountAppService.IsTenantAvailable(new IsTenantAvailableInput { TenancyName = loginModel.UsernameOrEmailAddress });
var userTenantId = usr.TenantId;
var tenancyNameOrNull = userTenantId.HasValue ? _tenantCache.GetOrNull((int)userTenantId)?.TenancyName : null;
var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, tenancyNameOrNull);`
-
0
Hi,
You can switch to host using below syntax;
using (_unitOfWorkManager.Current.SetTenantId(null)) { // your code... }
-
0
Hi,
I successfully detected the admin login but after logging in I get this error
Current user did not login to the application!
What am I missing?
-
0
Hi,
I'm stuck with this. Any help?
-
0
Still stuck. I need urgent assistance...
-
0
Hi @alfar_re
Could you share your final Login action ?
Thanks,
-
0
Hi @ismcagdas
Here you go.
public virtual async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "", string ss = "") { returnUrl = NormalizeReturnUrl(returnUrl); if (!string.IsNullOrWhiteSpace(returnUrlHash)) { returnUrl = returnUrl + returnUrlHash; } if (UseCaptchaOnLogin()) { await _recaptchaValidator.ValidateAsync(HttpContext.Request.Form[RecaptchaValidator.RecaptchaResponseKey]); } var usr = await _accountAppService.IsTenantAvailable(new IsTenantAvailableInput { TenancyName = loginModel.UsernameOrEmailAddress }); var userTenantId = usr.TenantId; var tenancyNameOrNull = userTenantId.HasValue ? _tenantCache.GetOrNull((int)userTenantId)?.TenancyName : null; var loginResult = await GetLoginResultAsync(loginModel.UsernameOrEmailAddress, loginModel.Password, tenancyNameOrNull); // if (!string.IsNullOrEmpty(ss) && ss.Equals("true", StringComparison.OrdinalIgnoreCase) && loginResult.Result == AbpLoginResultType.Success) { loginResult.User.SetSignInToken(); returnUrl = AddSingleSignInParametersToReturnUrl(returnUrl, loginResult.User.SignInToken, loginResult.User.Id, loginResult.User.TenantId); } if (_settingManager.GetSettingValue<bool>(AppSettings.UserManagement.AllowOneConcurrentLoginPerUser)) { await _userManager.UpdateSecurityStampAsync(loginResult.User); } if (loginResult.User.ShouldChangePasswordOnNextLogin) { loginResult.User.SetNewPasswordResetCode(); return Json(new AjaxResponse { TargetUrl = Url.Action( "ResetPassword", new ResetPasswordViewModel { TenantId = AbpSession.TenantId, UserId = loginResult.User.Id, ResetCode = loginResult.User.PasswordResetCode, ReturnUrl = returnUrl, SingleSignIn = ss }) }); } var signInResult = await _signInManager.SignInOrTwoFactorAsync(loginResult, loginModel.RememberMe); if (signInResult.RequiresTwoFactor) { return Json(new AjaxResponse { TargetUrl = Url.Action( "SendSecurityCode", new { returnUrl = returnUrl, rememberMe = loginModel.RememberMe }) }); } Debug.Assert(signInResult.Succeeded); await UnitOfWorkManager.Current.SaveChangesAsync(); return Json(new AjaxResponse { TargetUrl = returnUrl }); }
-
0
-
0
Hi,
I shared the project with you. I'm really stuck and would appreciate a resolution. Thanks.
-
0
Hi @ismcagdas,
Any help will be appreciated. I'm really stuck on this one.
-
0
Hi @alfar_re
Sorry, I couldn't see your email. Could you share the email subject and name of the project ?
Thanks,
-
0
Hi @ismcagdas,
Any assistance with this issue will be highly appreciated. This issue is affecting a deliverable.
-
0
Hi @alfar_re
It seems like the project link is expired. Could you share it again ? I will check this today.
Thanks,
-
0
Hi **@ismcagdas **,
I've reshared the link on mail with the same subject. Kindly check it.
Thank you.
-
0
Hi @ismcagdas,
Kindly confirm receipt of the email. I need to close this matter by tomorrow since there are a few development tasks being delayed by it. Please. Thank you.
-
0
Hi,
We got the project now, thanks.
-
0
Hi,
Great. Hoping for a resolution soon.
-
0
Hi @alfar_re
I have replied via email. We can continue with email conversation.