Portal password request - in progress.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Sarand.BusinessMgr.Net.Core.Web.App;
|
||||
using Sarand.BusinessMgr.Net.Core.Web.Helpers;
|
||||
using Sarand.BusinessMgr.Net.Core.Web.Portal.Models;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.App;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.Mgr;
|
||||
using Sarand.Net.Standard.Base.App;
|
||||
using Sarand.Net.Standard.Base.Bus;
|
||||
using Sarand.Net.Standard.Base.Security;
|
||||
@@ -36,7 +39,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
|
||||
// Check for 'Keep Logged In' cookie.
|
||||
// NB Only allowed if there isn't a current user session.
|
||||
if (!ValidUserSession && SessionConfig.Security.SignIn.KeepLoggedIn.Allowed)
|
||||
if (!ValidUserSession && AppConfig.Security.SignIn.KeepLoggedIn.Allowed)
|
||||
{
|
||||
String encryptedParams = GetRequestCookie(CookieName_KeepLoggedIn);
|
||||
KeepLoggedInParams keepLoggedInParams = ExtractKeepLoggedInParams(encryptedParams);
|
||||
@@ -44,24 +47,54 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
|
||||
if (SysId.IsValid(keepLoggedInParams.SecurityItemId) && SysId.IsValid(keepLoggedInParams.AffiliateId))
|
||||
{
|
||||
SaSecurityItem securityItemUser = SecurityConsole.GetSecurityItemById(SaSecurity.ItemType.User, keepLoggedInParams.SecurityItemId);
|
||||
if (securityItemUser != null)
|
||||
// User may be defined on any one of the BusinessMgr databases - need to find out which.
|
||||
// Identify session contexts with a BusMgr database that hosts the specified email address.
|
||||
List<ushort> sessionContextIdxs = SbmSessionContextHelper_Web.GetSessionContextIdxsForUsersBusMgrDatabase(keepLoggedInParams.EmailAddress);
|
||||
foreach (ushort sessionContextIdx in sessionContextIdxs)
|
||||
{
|
||||
SbmAffiliate userAccount = AccountMgr.GetAffiliateById(keepLoggedInParams.AffiliateId);
|
||||
if (userAccount != null)
|
||||
SbmSessionContext sessionContext = SbmSessionContextHelper_Web.GetSessionContext(sessionContextIdx);
|
||||
SbmUserContext_Web userContext = new SbmUserContext_Web(sessionContext);
|
||||
|
||||
using (SaSecurityConsole securityConsole = App.CreateSecurityConsole(userContext))
|
||||
{
|
||||
if (userAccount.IsStatusActive)
|
||||
SaSecurityItem securityItemUser = securityConsole.GetUserByEmail(keepLoggedInParams.EmailAddress);
|
||||
if (securityItemUser != null)
|
||||
{
|
||||
String errorMessage = "";
|
||||
if (SignUserIn(securityItemUser, userAccount, true, ref errorMessage))
|
||||
SaLogger.Info(fn + " Keep Logged In cookie found." + logSuffix);
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but failed to sign user in: " + errorMessage + "." + logSuffix);
|
||||
if (securityItemUser.Id.Equals(keepLoggedInParams.SecurityItemId))
|
||||
{
|
||||
using (SbmAccountMgr accountMgr = App.System.CreateAccountMgr(userContext))
|
||||
{
|
||||
SbmAffiliate affiliateUser = accountMgr.GetAffiliateById(keepLoggedInParams.AffiliateId);
|
||||
if (affiliateUser != null)
|
||||
{
|
||||
if (affiliateUser.Id.Equals(keepLoggedInParams.AffiliateId))
|
||||
{
|
||||
if (affiliateUser.UserId.Equals(securityItemUser.Id))
|
||||
{
|
||||
if (affiliateUser.IsStatusActive)
|
||||
{
|
||||
String errorMessage = "";
|
||||
if (SignUserIn(sessionContext, securityItemUser, affiliateUser, true, ref errorMessage))
|
||||
SaLogger.Info(fn + " Keep Logged In cookie found." + logSuffix);
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but failed to sign user in: " + errorMessage + "." + logSuffix);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Affiliate is not active." + logSuffix);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Affiliate User Id does not match security item." + logSuffix);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Affiliate does nto match Id." + logSuffix);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Affiliate not found." + logSuffix);
|
||||
}
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Security Item does not match Id." + logSuffix);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Affiliate is not active." + logSuffix);
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Security Item not found by email." + logSuffix);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Affiliate not found." + logSuffix);
|
||||
|
||||
if (ValidUserSession)
|
||||
break;
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Security Item not found." + logSuffix);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Keep Logged In cookie found but Security Item or Affiliate Ids missing." + logSuffix);
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
/// <para>- Open user session - store details against Sesion Data.</para>
|
||||
/// <para>- Set 'Keep logged in' cookie if required.</para>
|
||||
/// </summary>
|
||||
protected bool SignUserIn(SaSecurityItem securityItemUser, SbmAffiliate affiliateUser, bool keepLoggedIn, ref String errorMessage)
|
||||
protected bool SignUserIn(SbmSessionContext sessionContext, SaSecurityItem securityItemUser, SbmAffiliate affiliateUser, bool keepLoggedIn, ref String errorMessage)
|
||||
{
|
||||
String fn = GetType().Name + ".SignUserIn";
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
}
|
||||
|
||||
// 'Keep logged in' can be switched off by the application configuration.
|
||||
keepLoggedIn = App.SessionConfig.Security.SignIn.KeepLoggedIn.Allowed && keepLoggedIn;
|
||||
keepLoggedIn = App.AppConfig.Security.SignIn.KeepLoggedIn.Allowed && keepLoggedIn;
|
||||
|
||||
// Log user in.
|
||||
affiliateUser.KeepLoggedIn_Portal = keepLoggedIn;
|
||||
@@ -85,7 +85,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
SaLogger.Info(fn + " User session opened for " + affiliateUser.Email, true);
|
||||
|
||||
// Create session.
|
||||
OpenUserSession(SessionContext, securityItemUser, affiliateUser);
|
||||
OpenUserSession(sessionContext, securityItemUser, affiliateUser);
|
||||
|
||||
// Send the 'Keep Logged In' cookie if requested.
|
||||
if (affiliateUser.KeepLoggedIn_Portal)
|
||||
@@ -136,7 +136,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
protected void SetKeepLoggedInCookie()
|
||||
{
|
||||
String keepLoggedInCookieValue = GenerateKeepLoggedInCookieValue();
|
||||
int cookieExpiresDays = Math.Max(Sbm.Config.Security.SignIn.KeepLoggedIn.CookieExpiresDays_Min, Math.Min(Sbm.Config.Security.SignIn.KeepLoggedIn.CookieExpiresDays_Max, SessionConfig.Security.SignIn.KeepLoggedIn.CookieExpiresDays));
|
||||
int cookieExpiresDays = Math.Max(Sbm.Config.Security.SignIn.KeepLoggedIn.CookieExpiresDays_Min, Math.Min(Sbm.Config.Security.SignIn.KeepLoggedIn.CookieExpiresDays_Max, AppConfig.Security.SignIn.KeepLoggedIn.CookieExpiresDays));
|
||||
SetResponseCookie(CookieName_KeepLoggedIn, keepLoggedInCookieValue, DateTime.Now.AddDays(cookieExpiresDays), true);
|
||||
SaLogger.Info(this.GetType().Name + ".SetKeepLoggedInCookie 'Keep Logged In' cookie set. Value: " + keepLoggedInCookieValue);
|
||||
}
|
||||
@@ -164,11 +164,13 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Keep-logged-in cookie value.
|
||||
private static readonly String ParamName_KeepLoggedIn_EmailAddress = "ema";
|
||||
private static readonly String ParamName_KeepLoggedIn_SecurityItemId = "sii";
|
||||
private static readonly String ParamName_KeepLoggedIn_AffiliateId = "afi";
|
||||
private String GenerateKeepLoggedInCookieValue()
|
||||
{
|
||||
NameValueCollection keepLoggedInParams = new NameValueCollection();
|
||||
keepLoggedInParams.Add(ParamName_KeepLoggedIn_EmailAddress, SessionData_Portal.User.EmailAddress);
|
||||
keepLoggedInParams.Add(ParamName_KeepLoggedIn_SecurityItemId, SysId.ToString(SessionData_Portal.User.SecurityItemId));
|
||||
keepLoggedInParams.Add(ParamName_KeepLoggedIn_AffiliateId, SysId.ToString(SessionData_Portal.User.AffiliateId));
|
||||
return EncryptParameters(keepLoggedInParams, Base64IV_KeepLoggedIn, Base64Key_KeepLoggedIn);
|
||||
@@ -177,6 +179,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
//-------------------------------------------------------------------
|
||||
public class KeepLoggedInParams
|
||||
{
|
||||
public String EmailAddress { get; set; }
|
||||
public SysId SecurityItemId { get; set; }
|
||||
public SysId AffiliateId { get; set; }
|
||||
/// <summary>Retrieve the list of valid parameters in printable format [Name: Value].</summary>
|
||||
@@ -194,8 +197,9 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
if (!StringUtil.IsEmpty(encryptedParams))
|
||||
{
|
||||
NameValueCollection linkParams = DecryptParameters(encryptedParams, Base64IV_KeepLoggedIn, Base64Key_KeepLoggedIn);
|
||||
keepLoggedInParams.SecurityItemId = new SysId(StringUtil.ToInt(linkParams.Get(ParamName_KeepLoggedIn_SecurityItemId)));
|
||||
keepLoggedInParams.AffiliateId = new SysId(StringUtil.ToInt(linkParams.Get(ParamName_KeepLoggedIn_AffiliateId)));
|
||||
keepLoggedInParams.EmailAddress = linkParams.Get(ParamName_KeepLoggedIn_EmailAddress);
|
||||
keepLoggedInParams.SecurityItemId = linkParams.Get(ParamName_KeepLoggedIn_SecurityItemId).ToSysId();
|
||||
keepLoggedInParams.AffiliateId = linkParams.Get(ParamName_KeepLoggedIn_AffiliateId).ToSysId();
|
||||
}
|
||||
if (DebugMode) SaLogger.Info(GetType().Name + ".ExtractKeepLoggedInParams UserAccountId: " + SysId.ToString(keepLoggedInParams.AffiliateId));
|
||||
return keepLoggedInParams;
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Sarand.BusinessMgr.Net.Core.Web.App;
|
||||
using Sarand.BusinessMgr.Net.Core.Web.Helpers;
|
||||
using Sarand.BusinessMgr.Net.Core.Web.Portal.Models;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.App;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.Consoles;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.Helpers;
|
||||
using Sarand.BusinessMgr.Net.Standard.Base.Mgr;
|
||||
using Sarand.Net.Standard.Base.App;
|
||||
using Sarand.Net.Standard.Base.Bus;
|
||||
using Sarand.Net.Standard.Base.Core;
|
||||
@@ -43,23 +47,36 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
String messageTitle = Localiser["signin.caption"];
|
||||
|
||||
// Validate referrer.
|
||||
if (!ValidReferer)
|
||||
return Forbid();
|
||||
if (!ValidReferer) return Forbid();
|
||||
|
||||
// Validate Sign-in data.
|
||||
model.ValidateSignInData(responseData);
|
||||
if (responseData.HasControlErrors())
|
||||
return Json(responseData);
|
||||
|
||||
// Get user account.
|
||||
SaSecurityItem securityItemUser = GetSecurityItemForSignin(model, responseData);
|
||||
SbmAffiliate userAccount = GetAffiliateForSignin(model, securityItemUser, responseData);
|
||||
if (responseData.HasControlErrors())
|
||||
return Json(responseData);
|
||||
// Identify session contexts with a BusMgr database that hosts the specified email address.
|
||||
List<ushort> sessionContextIdxs = SbmSessionContextHelper_Web.GetSessionContextIdxsForUsersBusMgrDatabase(model.EmailAddress);
|
||||
foreach (ushort sessionContextIdx in sessionContextIdxs)
|
||||
{
|
||||
SbmSessionContext sessionContext = SbmSessionContextHelper_Web.GetSessionContext(sessionContextIdx);
|
||||
SbmUserContext_Web userContext = new SbmUserContext_Web(sessionContext);
|
||||
|
||||
String errorMessage = "";
|
||||
if (!SignUserIn(securityItemUser, userAccount, model.KeepMeSignedIn, ref errorMessage))
|
||||
return Json(responseData.SetControlError(nameof(model.EmailAddress), errorMessage));
|
||||
// Get user security item and affiliate.
|
||||
using (SaSecurityItem securityItemUser = GetSecurityItemForSignin(userContext, model, responseData))
|
||||
{
|
||||
if (responseData.HasControlErrors())
|
||||
return Json(responseData);
|
||||
using (SbmAffiliate affiliateUser = GetAffiliateForSignin(userContext, model, securityItemUser, responseData))
|
||||
{
|
||||
if (responseData.HasControlErrors())
|
||||
return Json(responseData);
|
||||
|
||||
String errorMessage = "";
|
||||
if (!SignUserIn(sessionContext, securityItemUser, affiliateUser, model.KeepMeSignedIn, ref errorMessage))
|
||||
return Json(responseData.SetControlError(nameof(model.EmailAddress), errorMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Construct response.
|
||||
responseData.Success = SessionData_Portal?.ValidUserSession ?? false;
|
||||
@@ -87,7 +104,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
if (!ValidUserSession)
|
||||
return Json(responseData.SetGoHome());
|
||||
|
||||
if (!SessionConfig.Security.SignIn.KeepLoggedIn.Allowed)
|
||||
if (!AppConfig.Security.SignIn.KeepLoggedIn.Allowed)
|
||||
return Json(responseData);
|
||||
|
||||
// Resend the cookie to extend the expiry date if it is currently populated.
|
||||
@@ -101,49 +118,51 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
public SaSecurityItem GetSecurityItemForSignin(Model_Security model, AjaxResponse_Data responseData)
|
||||
private SaSecurityItem GetSecurityItemForSignin(SbmUserContext_Web userContext, Model_Security model, AjaxResponse_Data responseData)
|
||||
{
|
||||
String fn = GetType().Name + ".GetSecurityItemForSignin";
|
||||
|
||||
// Retrieve security item by email address.
|
||||
SaSecurityItem.Params prmSecurityItem = new SaSecurityItem.Params();
|
||||
prmSecurityItem.SecurityItemType = SaSecurity.ItemType.User;
|
||||
prmSecurityItem.EmailAddress = model.EmailAddress;
|
||||
SaSecurityItem securityItemUser = SecurityConsole.GetSecurityItem(prmSecurityItem);
|
||||
|
||||
if (securityItemUser != null)
|
||||
SaSecurityItem securityItemUser = null;
|
||||
using (SaSecurityConsole securityConsole = App.CreateSecurityConsole(userContext))
|
||||
{
|
||||
// Validate password.
|
||||
if (!SbmPasswordHelper.VerifyHashedPassword(securityItemUser, model.Password) && !(DebugMode && model.EmailAddress.EqualsCI("ahoward@sarand.com")))
|
||||
// Retrieve security item by email address.
|
||||
securityItemUser = securityConsole.GetUserByEmail(model.EmailAddress);
|
||||
if (securityItemUser != null)
|
||||
{
|
||||
// Record invalid login attempt.
|
||||
//securityItemUser.InvalidLoginCount++;
|
||||
//securityItemUser.LastInvalidLoginAt = DateTime.Now;
|
||||
//if (!SecurityConsole.Update(securityItemUser, DbUpdateInfo))
|
||||
// SaLogger.Warn(fn + " Failed to store invalid sign-in attempt against User: " + model.EmailAddress + ". " + AccountMgr.ErrorStateMessage, true);
|
||||
SaLogger.Warn(fn + " Attempt to sign-in to User " + model.EmailAddress + " with invalid password."); // " Attempt: " + securityItemUser.InvalidLoginCount, true);
|
||||
securityItemUser = null;
|
||||
// Validate password.
|
||||
if (!SbmPasswordHelper.VerifyHashedPassword(securityItemUser, model.Password) && !(DebugMode && model.EmailAddress.EqualsCI("ahoward@sarand.com")))
|
||||
{
|
||||
// Record invalid login attempt.
|
||||
securityItemUser.InvalidLoginCount++;
|
||||
securityItemUser.LastInvalidLoginAt = DateTime.Now;
|
||||
if (!securityConsole.UpdateSecurityItem(securityItemUser, DbUpdateInfo))
|
||||
SaLogger.Warn(fn + " Failed to store invalid sign-in attempt against User: " + model.EmailAddress + ". " + AccountMgr.ErrorStateMessage, true);
|
||||
SaLogger.Warn(fn + " Attempt to sign-in to User " + model.EmailAddress + " with invalid password. Attempt: " + securityItemUser.InvalidLoginCount, true);
|
||||
responseData.SetControlError(nameof(model.EmailAddress), model.Localiser["security.msg.signin.invalid"]);
|
||||
securityItemUser = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return securityItemUser;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
public SbmAffiliate GetAffiliateForSignin(Model_Security model, SaSecurityItem securityItemUser, AjaxResponse_Data responseData)
|
||||
private SbmAffiliate GetAffiliateForSignin(SbmUserContext_Web userContext, Model_Security model, SaSecurityItem securityItemUser, AjaxResponse_Data responseData)
|
||||
{
|
||||
String fn = GetType().Name + ".GetAffiliateForSignin";
|
||||
|
||||
// Identify the contact asociated with the security item.
|
||||
SbmAffiliate.Params prmAffiliate = new SbmAffiliate.Params();
|
||||
prmAffiliate.UserId = securityItemUser.Id;
|
||||
prmAffiliate.StatusCat = SbmAffiliate.StatusCat.ActiveOnly;
|
||||
SbmAffiliate affiliate = AccountMgr.GetAffiliate(prmAffiliate);
|
||||
if (affiliate != null)
|
||||
SbmAffiliate affiliate = null;
|
||||
using (SbmAccountMgr accountMgr = App.System.CreateAccountMgr(userContext))
|
||||
{
|
||||
// Identify the contact asociated with the security item.
|
||||
SbmAffiliate.Params prmAffiliate = new SbmAffiliate.Params();
|
||||
prmAffiliate.UserId = securityItemUser.Id;
|
||||
prmAffiliate.StatusCat = SbmAffiliate.StatusCat.ActiveOnly;
|
||||
affiliate = accountMgr.GetAffiliate(prmAffiliate);
|
||||
if (affiliate != null)
|
||||
{
|
||||
}
|
||||
if (affiliate == null && !responseData.HasControlErrors())
|
||||
responseData.SetControlError(nameof(model.EmailAddress), model.Localiser["security.msg.signin.invalid"]);
|
||||
}
|
||||
if (affiliate == null && !responseData.HasControlErrors())
|
||||
responseData.SetControlError(nameof(model.EmailAddress), model.Localiser["security.msg.signin.invalid"]);
|
||||
return affiliate;
|
||||
}
|
||||
|
||||
@@ -171,8 +190,8 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
return Json(responseData.SetSessionExpired(messageTitle, Localiser["security.msg.warn.sessionExpired"]));
|
||||
|
||||
// Retrieve user account - in case sign-out needs to be recorded.
|
||||
SbmAffiliate userAccount = AccountMgr.GetAffiliateById(SessionData_Portal.User.AffiliateId);
|
||||
if (userAccount == null)
|
||||
SbmAffiliate affiliateUser = AccountMgr.GetAffiliateById(SessionData_Portal.User.AffiliateId);
|
||||
if (affiliateUser == null)
|
||||
{
|
||||
SaLogger.Warn(fn + " System error - failed to retrieve account details.");
|
||||
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
|
||||
@@ -339,35 +358,54 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
String messageTitle = Localiser["security.caption.request.password"];
|
||||
|
||||
// Validate referrer.
|
||||
if (!ValidReferer)
|
||||
return Forbid();
|
||||
if (!ValidReferer) return Forbid();
|
||||
|
||||
// Validate data.
|
||||
model.ValidateRequestPasswordData(responseData);
|
||||
if (responseData.HasControlErrors())
|
||||
return Json(responseData);
|
||||
if (responseData.HasControlErrors()) return Json(responseData);
|
||||
|
||||
// Retrieve user account.
|
||||
using (SaSecurityItem securityItemUser = SecurityConsole.GetUserByEmail(model.EmailAddress))
|
||||
using (SbmAffiliate affiliateUser = AccountMgr.GetAffiliateByUserId(securityItemUser?.Id ?? null))
|
||||
// Identify session contexts with a BusMgr database that hosts the specified email address.
|
||||
List<ushort> sessionContextIdxs = SbmSessionContextHelper_Web.GetSessionContextIdxsForUsersBusMgrDatabase(model.EmailAddress);
|
||||
foreach (ushort sessionContextIdx in sessionContextIdxs)
|
||||
{
|
||||
if (securityItemUser == null)
|
||||
SaLogger.Warn(fn + " Unable to send password request email to " + model.EmailAddress + " - security item does not exist");
|
||||
else if (affiliateUser == null)
|
||||
SaLogger.Warn(fn + " Unable to send password request email to " + model.EmailAddress + " - account does not exist");
|
||||
else if (!affiliateUser.IsStatusActive)
|
||||
SaLogger.Warn(fn + " Unable to send password request email to " + model.EmailAddress + " - account is not active");
|
||||
else
|
||||
SbmSessionContext sessionContext = SbmSessionContextHelper_Web.GetSessionContext(sessionContextIdx);
|
||||
SbmUserContext_Web userContext = new SbmUserContext_Web(sessionContext);
|
||||
|
||||
// Get user security item and affiliate.
|
||||
using (SaSecurityConsole securityConsole = App.CreateSecurityConsole(userContext))
|
||||
{
|
||||
// Send password request email.
|
||||
SaEmailData emailData = new SaEmailData();
|
||||
emailData.Config = EmailConfig;
|
||||
emailData.AddressesTo.Add(model.EmailAddress);
|
||||
emailData.Subject = Localiser["security.caption.request.password"];
|
||||
PopulateRequestPasswordEmailBody(emailData, securityItemUser, affiliateUser);
|
||||
if (EmailConsole.SendEmail(emailData))
|
||||
SaLogger.Info(fn + " Password request email sent to " + model.EmailAddress, true);
|
||||
else SaLogger.Warn(fn + " Failed to send password request email to " + model.EmailAddress);
|
||||
using (SaSecurityItem securityItemUser = securityConsole.GetUserByEmail(model.EmailAddress))
|
||||
{
|
||||
if (securityItemUser != null)
|
||||
{
|
||||
using (SbmAccountMgr accountMgr = App.System.CreateAccountMgr(userContext))
|
||||
{
|
||||
using (SbmAffiliate affiliateUser = accountMgr.GetAffiliateByUserId(securityItemUser.Id))
|
||||
{
|
||||
if (affiliateUser != null)
|
||||
{
|
||||
if (affiliateUser.Status == SaStatus.Current || affiliateUser.IsStatusActive)
|
||||
{
|
||||
SbmEmailConsole emailConsole = App.CreateEmailConsole(userContext);
|
||||
|
||||
// Send password request email.
|
||||
SaEmailData emailData = new SaEmailData();
|
||||
emailData.Config = App.EmailConfig;
|
||||
emailData.AddressesTo.Add(model.EmailAddress);
|
||||
emailData.Subject = Localiser["security.caption.request.password"];
|
||||
PopulateRequestPasswordEmailBody(emailConsole, userContext, emailData, securityItemUser, affiliateUser);
|
||||
if (emailConsole.SendEmail(emailData))
|
||||
SaLogger.Info(fn + " Password request email sent to " + model.EmailAddress, true);
|
||||
else SaLogger.Warn(fn + " Failed to send password request email to " + model.EmailAddress);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Unable to send password request email to " + model.EmailAddress + " - affiliate is not active");
|
||||
}
|
||||
else SaLogger.Warn(fn + " Unable to send password request email to " + model.EmailAddress + " - affiliate does not exist. UserId: " + SysId.ToString(securityItemUser.Id));
|
||||
}
|
||||
}
|
||||
}
|
||||
else SaLogger.Warn(fn + " Unable to send password request email to " + model.EmailAddress + " - security item does not exist.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,17 +414,17 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
responseData.SuccessMsg = Localiser["security.msg.request.password.email.sent"];
|
||||
return Json(responseData);
|
||||
}
|
||||
private void PopulateRequestPasswordEmailBody(SaEmailData emailData, SaSecurityItem securityItemUser, SbmAffiliate userAccount)
|
||||
private void PopulateRequestPasswordEmailBody(SbmEmailConsole emailConsole, SbmUserContext userContext, SaEmailData emailData, SaSecurityItem securityItemUser, SbmAffiliate affiliateUser)
|
||||
{
|
||||
// Will need to handle culture (language/country) as and when introduced.
|
||||
emailData.MessageBody = SbmResourceHelper.GetTextResource(SbmResourceHelper.TextResource.Email_RequestPassword_Html);
|
||||
emailData.HtmlBody = true;
|
||||
|
||||
// Replace dynamic tags.
|
||||
EmailConsole.ReplaceDynamicEmailBodyTags(emailData, SessionData);
|
||||
emailConsole.ReplaceDynamicEmailBodyTags(emailData, null);
|
||||
|
||||
// Replace dynamic tag for link back to change password page.
|
||||
SaEmailConsole.ReplaceDynamicEmailTag(emailData, "[#Link.RequestPassword]", GenerateRequestPasswordLink(securityItemUser.Id, userAccount.Id, securityItemUser.EmailAddress, ""));
|
||||
SaEmailConsole.ReplaceDynamicEmailTag(emailData, "[#Link.RequestPassword]", GenerateRequestPasswordLink(securityItemUser.Id, affiliateUser.Id, securityItemUser.EmailAddress, ""));
|
||||
SaEmailConsole.ReplaceDynamicEmailTag(emailData, "[#Link.ExpiresMinutes]", GetRequestPasswordEmailLinkExpiresMinutes().ToString());
|
||||
}
|
||||
|
||||
@@ -401,8 +439,8 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
// NB IV value is generated from the key and fixed so that it doesn't change when the application restarts.
|
||||
// IV Must be regenerated if key is changed.
|
||||
private static String Base64Key_RequestPassword { get; } = SacsEncryptionHelper.EncodePlainKey("[-SaBusMgr+<Request=Password>!^]", SacsEncryptionHelper.KeyLength);
|
||||
private static String Base64IV_RequestPassword_Generated { get; } = SacsEncryptionHelper.InitSymmetricEncryptionIV(Base64Key_RequestPassword);
|
||||
private static String Base64IV_RequestPassword { get; } = "1VAWYTW6d72n/6HDNhbqow==";
|
||||
//private static String Base64IV_RequestPassword_Generated { get; } = SacsEncryptionHelper.InitSymmetricEncryptionIV(Base64Key_RequestPassword);
|
||||
private static String Base64IV_RequestPassword { get; } = "iALRIxzrZIfrB4boNFRXfA==";
|
||||
private String GenerateRequestPasswordLink(SysId securityItemId, SysId affiliateId, String emailAddress, String requestPasswordToken)
|
||||
{
|
||||
NameValueCollection parameters = new NameValueCollection();
|
||||
@@ -418,7 +456,7 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
}
|
||||
private int GetRequestPasswordEmailLinkExpiresMinutes()
|
||||
{
|
||||
int expiresMinutes = SessionConfig.Security.SignIn.PasswordRequest.EmailLinkExpiresMins;
|
||||
int expiresMinutes = AppConfig.Security.SignIn.PasswordRequest.EmailLinkExpiresMins;
|
||||
int expiresMinutes_Min = Sbm.Config.Security.SignIn.PasswordRequest.EmailLinkExpiresMins_Min;
|
||||
int expiresMinutes_Max = Sbm.Config.Security.SignIn.PasswordRequest.EmailLinkExpiresMins_Max;
|
||||
return Math.Max(expiresMinutes_Min, Math.Min(expiresMinutes_Max, expiresMinutes));
|
||||
@@ -456,13 +494,13 @@ namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
||||
DateUtil.IsValid(ExpiresAt) && ExpiresAt >= DateTime.Now;
|
||||
}
|
||||
/// <summary>Verify that the data is valid and the account details match.</summary>
|
||||
public bool IsValid(SaSecurityItem securityItemUser, SbmAffiliate userAccount)
|
||||
public bool IsValid(SaSecurityItem securityItemUser, SbmAffiliate affiliateUser)
|
||||
{
|
||||
return
|
||||
IsValid() &&
|
||||
userAccount != null &&
|
||||
affiliateUser != null &&
|
||||
securityItemUser.Id.Equals(SecurityItemId) &&
|
||||
userAccount.Id.Equals(AffiliateId) &&
|
||||
affiliateUser.Id.Equals(AffiliateId) &&
|
||||
securityItemUser.EmailAddress.EqualsCI(EmailAddress);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user