public int?GetCurrentUserId() { var cachedUserId = _inMemoryUserSessionService.GetCurrentUserId(); if (cachedUserId.HasValue) { return(cachedUserId); } var user = _httpContextAccessor?.HttpContext?.User; var userIdClaim = user?.FindFirst(CofoundryClaimTypes.UserId); var userAreaClaim = user?.FindFirst(CofoundryClaimTypes.UserAreaCode); if (userIdClaim == null || userAreaClaim == null) { return(null); } // User has been signed out during this request so ignore if (_signedOutUserAreas.Contains(userAreaClaim.Value)) { return(null); } // Otherwise get it from the Identity var userId = IntParser.ParseOrNull(userIdClaim.Value); return(userId); }
/// <summary> /// Gets the UserId of the currently logged in user for a specific UserArea, /// regardless of the ambient authentication scheme. Useful in multi-userarea /// scenarios where you need to ignore the ambient user and check for permissions /// against a specific user area. /// </summary> /// <param name="userAreaCode">The unique identifying code fo the user area to check for.</param> public async Task <int?> GetUserIdByUserAreaCodeAsync(string userAreaCode) { if (userAreaCode == null) { throw new ArgumentNullException(nameof(userAreaCode)); } if (cachedUserIdArea == userAreaCode && userIdCache.HasValue) { return(userIdCache.Value); } var scheme = CofoundryAuthenticationConstants.FormatAuthenticationScheme(userAreaCode); var result = await _httpContextAccessor.HttpContext.AuthenticateAsync(scheme); if (!result.Succeeded) { return(null); } var userIdClaim = result.Principal.FindFirst(ClaimTypes.NameIdentifier); if (userIdClaim == null) { return(null); } var userId = IntParser.ParseOrNull(userIdClaim.Value); return(userId); }
public static DateTime?GuessBirthDate(string number, DateTime?overrideNow = null) { if (IsFnr(number) == false && IsDNumber(number) == false) { return(null); } var d = IntParser.ParseOrNull(number.Substring(0, 2)); var m = IntParser.ParseOrNull(number.Substring(2, 2)); var y = IntParser.ParseOrNull(number.Substring(4, 2)); if (IsDNumber(number)) { d -= 40; } var birthDate = DateTimeParser.ParseOrNull(d.ToString().PadLeft(2, '0') + m.ToString().PadLeft(2, '0') + "19" + y.ToString().PadLeft(2, '0'), "ddMMyyyy"); if (birthDate.HasValue == false) { return(null); } const int maxYearsGuess = 100; var today = overrideNow ?? DateTime.Today; if (today.Year - birthDate.Value.Year >= maxYearsGuess) { birthDate = birthDate.Value.AddYears(maxYearsGuess); } return(birthDate); }
private async Task <int?> GetCurrentlySignedInUserId(HttpClient client) { var response = await client.GetAsync("/tests/users/current"); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsStringAsync(); return(IntParser.ParseOrNull(result)); }
/// <summary> /// Creates DbUpateCommands from sql scripts embedded in an assembly. Only new schema updates /// are included and functions/sps/triggers etc are only returned if there are any relevant schema /// updates, so you need to create a schema update file if you want to force these to update. /// </summary> /// <param name="assembly">The assembly to scan for sql scripts.</param> /// <param name="currentVersion">The current version of the module</param> /// <param name="scriptPath">The folder path of the script files which defaults to 'Install.Db.' (which equates to 'Install/Db/')</param> /// <returns>Collecton of IUpdateCommands that represents all the required db updates</returns> public IEnumerable <IVersionedUpdateCommand> Create(Assembly assembly, ModuleVersion currentVersion, string scriptPath = "Install.Db.") { Condition.Requires(assembly).IsNotNull(); var scriptFiles = GetScripts(assembly, scriptPath); var commands = new List <UpdateDbCommand>(); int maxVersionNumber = 0; // Get schema scripts we need so we know the version number. foreach (var scriptFile in scriptFiles.Where(s => s.Contains(".Schema."))) { var fileName = GetScriptFileName(scriptFile); var version = IntParser.ParseOrNull(fileName); if (!version.HasValue) { throw new InvalidCastException("Unable to parse version number from schema update file: " + scriptFile); } if (currentVersion != null && version.Value <= currentVersion.Version) { continue; } var command = new UpdateDbCommand(); command.Version = version.Value; command.ScriptType = DbScriptType.Schema; command.Sql = GetResource(assembly, scriptFile); command.Description = scriptFile; command.FileName = fileName; commands.Add(command); if (maxVersionNumber < version.Value) { maxVersionNumber = version.Value; } } if (!commands.Any()) { return(Enumerable.Empty <IVersionedUpdateCommand>()); } foreach (var scriptFile in scriptFiles.Where(s => !s.Contains(".Schema."))) { var command = new UpdateDbCommand(); command.Version = maxVersionNumber; command.Sql = GetResource(assembly, scriptFile); command.ScriptType = MapScriptType(scriptFile); command.Description = scriptFile; command.FileName = GetScriptFileName(scriptFile); commands.Add(command); } return(commands); }
private VisualEditorRequestParameters GetRequestParameters() { var requestParameters = new VisualEditorRequestParameters(); var request = _httpContextAccessor.HttpContext?.Request; if (request != null) { requestParameters.VisualEditorMode = request.Query["mode"]; requestParameters.VersionId = IntParser.ParseOrNull(request.Query["version"]); } return(requestParameters); }
public async Task <int?> GetUserIdByUserAreaCodeAsync(string userAreaCode) { if (userAreaCode == null) { throw new ArgumentNullException(nameof(userAreaCode)); } if (_signedOutUserAreas.Contains(userAreaCode)) { return(null); } var cachedUserId = await _inMemoryUserSessionService.GetUserIdByUserAreaCodeAsync(userAreaCode); if (cachedUserId.HasValue) { return(cachedUserId); } if (_httpContextAccessor.HttpContext == null) { return(null); } var scheme = AuthenticationSchemeNames.UserArea(userAreaCode); var result = await _httpContextAccessor.HttpContext.AuthenticateAsync(scheme); if (!result.Succeeded) { return(null); } var userIdClaim = result.Principal.FindFirst(CofoundryClaimTypes.UserId); if (userIdClaim == null) { return(null); } var userId = IntParser.ParseOrNull(userIdClaim.Value); if (userId.HasValue) { // cache the auth by logging into the in-memory service await _inMemoryUserSessionService.SignInAsync(userAreaCode, userId.Value, true); } return(userId); }
/// <summary> /// Gets the UserId of the user currently logged /// in to this session /// </summary> /// <returns> /// UserId of the user currently logged /// in to this session /// </returns> public int?GetCurrentUserId() { // Use the cache if it has been set if (userIdCache.HasValue) { return(userIdCache); } if (HttpContext.Current == null || HttpContext.Current.User == null || !HttpContext.Current.User.Identity.IsAuthenticated) { return(null); } // Otherwise get it from the Identity var userId = IntParser.ParseOrNull(HttpContext.Current.User.Identity.Name); return(userId); }
/// <summary> /// Gets the UserId of the user authenticated for the /// current request under the ambient authentication scheme. /// </summary> /// <returns> /// Integer UserId or null if the user is not logged in for the ambient /// authentication scheme. /// </returns> public int?GetCurrentUserId() { if (userIdCache.HasValue) { return(userIdCache); } var user = _httpContextAccessor.HttpContext?.User; var userIdClaim = user.FindFirst(ClaimTypes.NameIdentifier); if (userIdClaim == null) { return(null); } // Otherwise get it from the Identity var userId = IntParser.ParseOrNull(userIdClaim.Value); return(userId); }
public bool Verify(string password, string hash) { Condition.Requires(hash).IsNotNullOrWhiteSpace(); Condition.Requires(password).IsNotNullOrEmpty(); // First 16 characters of the hash are the base64 encoded salt // BUT salt isn't always 16 characters long because the old method allowed for negative // numbers and some therefore some are prepended the minus sign so we add some padding var saltPart = hash.Substring(0, SALT_LENGTH).Trim(SALT_PAD_CHAR); var decodedSaltString = Encoding.UTF8.GetString(Convert.FromBase64String(saltPart)); var salt = IntParser.ParseOrNull(decodedSaltString); if (!salt.HasValue) { throw new ArgumentException("Hash is not correctly formatted"); } var hashCheck = CreateHash(password, salt.Value); return(hash == hashCheck); }
public static bool?IsMale(string fnr) { return(IsFnr(fnr) ? IntParser.ParseOrNull(fnr.Substring(8, 1)) % 2 != 0 : (bool?)null); }