如何获取用户管理器 。 FindByEmail () 作为要使用 signIInManager 的选中电话 。 checkPasswordSignIn ()?
原标题:How to fetch UserManager.FindByEmail() as a select call to use with SignInManager.CheckPasswordSignIn()?
问题回答
To solve my issue I just changed this code below. Now I only fetch once from the DB, instead of 3 times.
Maybe someone who knows UserManager can weigh in on the difference between what I had and what I have now in terms of security?
// Old Code
var userFromRepo = await _userManager.FindByEmailAsync(loginDto.Email);
// New code
var user = await _userManager.Users
.Include(p => p.TokensPoints)
.Include(p => p.UserRoles)
.ThenInclude(r => r.Role)
.SingleOrDefaultAsync(x => x.NormalizedEmail == loginDto.Email);
next I just map the fields I want to the DTO being returned to the client.
If you want to extend the _userManager.FindByEmailAsync method, you need to create a custom class which inherts the IUserEmailStore, since the FindByEmailAsync method will call the IUserEmailStore s FindByEmailAsync method.
The source codes for the _userManager is like this:
public virtual async Task FindByEmailAsync(string email)
{
ThrowIfDisposed();
IUserEmailStore store = GetEmailStore();
Microsoft.AspNetCore.Shared.ArgumentNullThrowHelper.ThrowIfNull(email, "email");
email = NormalizeEmail(email);
TUser val = await store.FindByEmailAsync(email, CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
if (val == null && Options.Stores.ProtectPersonalData)
{
ILookupProtectorKeyRing service = _services.GetService();
ILookupProtector protector = _services.GetService();
if (service != null && protector != null)
{
foreach (string allKeyId in service.GetAllKeyIds())
{
string normalizedEmail = protector.Protect(allKeyId, email);
val = await store.FindByEmailAsync(normalizedEmail, CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
if (val != null)
{
return val;
}
}
}
}
return val;
}
Inside the new implemented IUserEmailStore class ,you could modify the FindByEmailAsync like below:
_dbContext.Users
.Where(u => u.Email == email)
.Select(p => new UserDto
{
Id = p.Id,
Email = p.Email,
UserName = p.UserName,
Hosted = p.Hosted,
Instructed = p.Instructed,
Attended = p.Attended,
IsBoarded = p.IsBoarded,
IsActive = p.IsActive,
Likers = p.LikersCount,
Rating = p.Rating,
YearStarted = p.YearStarted,
YearsInstructing = p.YearsInstructing,
YearsPracticing = DateTime.Now.Year - p.YearStarted,
Certification = p.Certification.ToString(),
CreatedDate = p.CreatedDate,
PhotoUrl = p.IsBoarded ? p.UserPhoto : "assets/images/user.png",
Age = p.DateOfBirth.CalculateAge(),
DateOfBirth = p.DateOfBirth,
ExperienceLevel = p.ExperienceLevel.GetEnumName(),
Points = p.UserPoints.Sum(up => up.Points),
Tokens = p.Tokens.Sum(t => t.Tokens),
Token = _tokenService.CreateToken(p,
(from userRole in p.UserRoles
join role in _dbContext.Roles on userRole.RoleId equals role.Id
select role.Name).ToList()),
IsInstructor = p.IsInstructor
})
.FirstOrDefaultAsync();
Then register it inside the program.cs:
builder.Services.AddTransient, CustomUserEmailStore>();
More details about how to use it, you could refer to this article.
You do not have to call the user again the user already exist on userfromrepo.
Just preform your logic on your current user object, userfromrepo.column name = what ever value. It will not update the database. Just the current user object and return it.
相关问题
What is the use of `default` keyword in C#?
What is the use of default keyword in C#?
Is it introduced in C# 3.0 ?
Anyone feel like passing it forward?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs.
...
NSArray s, Primitive types and Boxing Oh My!
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits.
Now here s the question: I feel really inclined to create some type of ...
C# Marshal / Pinvoke CBitmap?
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class.
My import looks like this:
[DllImport(@"test.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ...
ADO.NET Entity Framework Association of Entities by Value Range
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber.
I want to create a many to many association ...
How to Use Ghostscript DLL to convert PDF to PDF/A
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
What is the most efficient keyvalue pair for ordering?
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Linqy no matchy
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control.
...