目前,我在我的系统中使用Asp.net核心1.1,EF核心和Asp.net核心身份.我在startup.cs课堂上有一个配置密码策略.
是否有配置在用户连续登录失败时禁用帐户?
services.Configure<IdentityOptions>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = true;
}
Run Code Online (Sandbox Code Playgroud) c# asp.net-identity entity-framework-core asp.net-core asp.net-core-identity
就像标题一样,我试图强迫用户在让他登录之前确认电子邮件。我是根据 Microsoft 教程做的事情,并且有人写到我必须添加
o.SignIn.RequireConfirmedEmail = true;
我做了什么,但它不会阻止我登录,即使我没有确认我的电子邮件
services.AddIdentity<Company, IdentityRole>
(o =>
{
// configure identity options
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 6;
o.SignIn.RequireConfirmedEmail = true;
o.Tokens.EmailConfirmationTokenProvider = EmailConfirmationTokenProviderName;
})
.AddEntityFrameworkStores<ShopContext>()
.AddTokenProvider<ConfirmEmailDataProtectorTokenProvider<Company>>(EmailConfirmationTokenProviderName);
Run Code Online (Sandbox Code Playgroud)
我正在使用 jwt 令牌身份验证在这种情况下我是否做了比我展示的更多的事情?
我正在尝试CustomRequestCultureProvider在我的本地化管道中实现 a ,以从数据库中检索用户文化。为了识别用户,我需要ClaimsPrincipal,但由于某种原因,即使用户已通过身份验证,context.User.Identity.IsAuthenticated也始终为 false。
这是相关代码(被截断以突出我的问题:身份验证已经在工作;本地化代码基于本地化教程)。
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("fr"),
new CultureInfo("en"),
};
options.DefaultRequestCulture = new RequestCulture("fr");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
// we insert at 2, because we want to first check the query, then the cookie, then the DB, then the headers
options.RequestCultureProviders.Insert(2, new CustomRequestCultureProvider(async context =>
{
// ----->> ISSUE IS HERE: this is …Run Code Online (Sandbox Code Playgroud) asp.net-core asp.net-core-localization asp.net-core-identity
在Asp.net core mvc项目中,如何在View中获取当前的IdentityUser信息?
如何在Asp core 2.x中通过admin更改用户密码?
或使用短信代码更改密码
我的示例代码:
if (!ModelState.IsValid)
return View(model);
var user = await _userManager.FindByNameAsync(model.UserName);
if (user == null)
return RedirectToAction("Index");
if (model.smsCode == user.SmsCode)
{
user.PasswordHash = model.NewPassword;
IdentityResult result = await _userManager.UpdateAsync(user);
if (result.Succeeded)
{
}
}
Run Code Online (Sandbox Code Playgroud)
错误:在数据库中保存散列传递
登录失败时,我想知道它是用户名,密码还是其他。
var signinResult = await _signInManager.PasswordSignInAsync(
loginViewModel.UserName,
loginViewModel.Password,
false, false);
Run Code Online (Sandbox Code Playgroud)
返回SignInResult,它仅告诉我它是NotAllowed。
我能以某种方式从身份中获得更有意义的理由吗?
在我的播种代码中,我创建了用户和角色,错误似乎是每当我通过启动运行它时,我似乎都会得到
“没有
Microsoft.AspNetCore.Identity.RoleManager'1[Microsoft.AspNetCore.Identity.IdentityRole]注册类型服务。
编辑*** 我认为问题是 identityhostingstartup 中的代码没有被执行,这是由脚手架身份创建的。
种子数据库
public static void CreateRolesAndUsers(IServiceProvider serviceProvider)
{
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var userManager = serviceProvider.GetRequiredService<UserManager<AppUser>>();
var context = serviceProvider.GetService<AuthContext>();
var configuration = serviceProvider.GetService<IConfiguration>();
//context.Database.EnsureCreated();
context.Database.Migrate();
Task<IdentityResult> roleResult;
string email = configuration["Admin:Email"];
//Check that there is an Administrator role and create if not
Task<bool> hasAdminRole = roleManager.RoleExistsAsync(Roles.ADMIN);
hasAdminRole.Wait();
if (!hasAdminRole.Result)
{
roleResult = roleManager.CreateAsync(new IdentityRole(Roles.ADMIN));
roleResult.Wait();
}
//Check if the admin user exists and create it if not
//Add to the Administrator role …Run Code Online (Sandbox Code Playgroud) 我正在将数以万计的用户从数据库中没有密码的旧网站迁移到这个新的 Web 应用程序,但是,当我尝试使用异步方法导入用户时,最终需要几天的时间几天后我最终取消了它。
现在我已经求助于直接从 _context.Users.Add 创建新用户并分配他们的角色,我可以毫无问题地做到这一点..但是,我似乎无法弄清楚如何创建通用密码(都是一样的)密码),因为这些用户只会获得一个密码来观看直播(不需要超级安全),但我仍然需要管理员帐户的安全部分,通过客户端/管理员端 UI 处理其他内容。如果用户登录,我会自动为他们输入默认密码。
但由于某种原因,我无法让密码哈希器正常工作,因为当我登录时,它说密码错误......
这就是我用来生成密码和创建用户的...
var appUser = new ApplicationUser() {
Id = GenerateId(),
AccessFailedCount = 0,
Email = user[1],
PasswordHash = "",
FullName = "Standard User",
UserName = user[1],
PhoneNumber = user[8],
FirstName = user[2],
LastName = user[3],
JoinMailingList = user[4],
Country = user[5],
City = user[6],
StateRegion = user[7]
};
_context.Users.Add(appUser);
var options = new PasswordHasherOptions();
options.CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2;
var hasher = new PasswordHasher < ApplicationUser > ();
appUser.PasswordHash = hasher.HashPassword(appUser, "Default8!");
var …Run Code Online (Sandbox Code Playgroud) 运行项目时,遇到这个问题:(我用过asp.net core 3。)
无法从根提供程序解析范围服务“Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User]”。
我怎么解决这个问题?
ApplicationDbContext 类:
public class ApplicationDbContext : IdentityDbContext<User, Role, int,
UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public static async Task CreateAdminAccount(IServiceProvider
serviceProvider, IConfiguration configuration)
{
UserManager<User> userManager =
serviceProvider.GetRequiredService<UserManager<User>>();
RoleManager<Role> roleManager =
serviceProvider.GetRequiredService<RoleManager<Role>>();
string userName = configuration["Data:AdminUser:Name"];
string email = configuration["Data:AdminUser:Email"];
string password = configuration["Data:AdminUser:Password"];
string role = configuration["Data:AdminUser:Role"];
if (await userManager.FindByNameAsync(userName) == null)
{
if (await roleManager.FindByNameAsync(role) == null)
{
await roleManager.CreateAsync(new Role(role));
}
User user …Run Code Online (Sandbox Code Playgroud) c# entity-framework-core asp.net-core asp.net-core-identity asp.net-core-3.0
即使在注册 AddDefaultIdentity 后,我也遇到异常
System.InvalidOperationException: Service type: IUserClaimsPrincipalFactory`1 not registered.
Run Code Online (Sandbox Code Playgroud)
这是身份注册码:
services.AddDefaultIdentity<ApplicationUser>(options =>
{
//Disable account confirmation.
options.SignIn.RequireConfirmedAccount = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)
这是 IdentityServer 代码:
var builder = services.AddIdentityServer()
.AddInMemoryApiScopes(IdentityServerConfig.ApiScopes)
.AddInMemoryIdentityResources(IdentityServerConfig.IdentityResources)
.AddInMemoryApiResources(IdentityServerConfig.ApiResources)
.AddInMemoryClients(IdentityServerConfig.Clients)
.AddAspNetIdentity<IdentityUser>();
Run Code Online (Sandbox Code Playgroud)