标签: asp.net-identity

生成 PasswordHash 和 SecurityStamp

我有一个Registrations新用户可以放入的表。稍后的过程为该用户创建一个数据库,并从注册表(电子邮件和姓名)中的数据插入一条 ASP.NET 标识用户记录。

我想对此进行扩展,以便在注册时,用户可以输入他们的密码,然后将在新数据库中设置密码。

要正确执行此操作,我需要创建一个SecurityStamp值,然后使用该值加密密码以获取PasswordHash. 然后我将这些值存储在注册表中,然后我可以在设置时将它们复制到用户的新数据库中,他们将能够使用他们注册的密码登录。

我将如何做到这一点 - 生成SecurityStamp密码然后散列密码?

asp.net asp.net-identity

2
推荐指数
1
解决办法
8092
查看次数

MVC 5 中带有 ASP.NET 标识的 Autofac 不验证 OWIN 管道中的安全标记

我将 AutoFac 设置为与 MVC 5 中的 ASP.NET Identity 一起使用。表面上似乎一切正常,即用户可以创建帐户并登录。但后来我发现当安全标记更改时用户不会被注销。通过 AspNetUsers 表中的蛮力或用户更改密码并期望在其他浏览器中注销。

这就是我按照这篇非官方文章设置 AutoFac 的方式。

public void Configuration(IAppBuilder app)
{
    var builder = new ContainerBuilder();

    builder.RegisterType<ApplicationDbContext>().AsSelf().InstancePerRequest();
    builder.RegisterType<ApplicationUserStore>().As<IUserStore<ApplicationUser>>().InstancePerRequest();
    builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
    builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
    builder.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
    builder.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();

    builder.RegisterControllers(typeof(MvcApplication).Assembly);

    var container = builder.Build();

    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    app.UseAutofacMiddleware(container);
    app.UseAutofacMvc();

    ConfigureAuth(app);
}
Run Code Online (Sandbox Code Playgroud)

这就是我设置 cookie 身份验证中间件的方式。除了验证间隔更短的时间跨度外,它是默认值。

public void ConfigureAuth(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromSeconds(15),
                regenerateIdentity: (manager, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc autofac owin asp.net-identity

2
推荐指数
1
解决办法
1010
查看次数

密码重置时 ASP.Net 标识“无效令牌”,密码中带有 *

当用户在输入新密码后尝试在重置密码屏幕上重置密码时,我们会收到无效令牌错误消息。通常这对每个人都很好,即使有一些特殊的字符,比如#。我们现在有一个案例,某人在重置密码屏幕上将 * 输入到他的新密码中,只是因为这个特殊字符而收到此错误消息。

我现在已经尝试了数小时的研究来找到为什么会发生这种情况的解决方案,但没有运气。我在这里找到了这个解决方案,它在用户名中存在特殊字符的问题,但我们没有这个问题。只有密码中的那个特殊字符有问题。由于我们已经在生产中,我们不能只禁止在密码中使用该字符。

有人有线索吗?

生成令牌控制器方法:

[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await _userManager.FindByNameAsync(model.Email.ToLower());
        if (user == null || !(await _userManager.IsEmailConfirmedAsync(user.UserName)))
        {
            // Don't reveal that the user does not exist or is not confirmed
            return View("ForgotPasswordConfirmation");
        }

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
        // Send an email with this link
        var code = await _userManager.GeneratePasswordResetTokenAsync(user.UserName);
        code = …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net passwords asp.net-identity

2
推荐指数
1
解决办法
4381
查看次数

支持在 ASP.NET Core 中为同一用户使用电子邮件或用户名登录

我将 ASP.NET Core Identity (2.0) 与所有默认代码一起使用。我改变了AccountController.Login检查旧数据库并在找不到用户时迁移用户方法。由于旧数据库同时具有用户名和电子邮件,我将在新创建的用户中填充这两个字段。

但是,如果我尝试使用迁移的用户再次登录,则使用电子邮件不起作用,因为似乎 SQL 请求总是WHERE u.NormalizedUserName = @__normalizedUserName_0只查找。

如何启用电子邮件和用户名不同的单个用户使用他们的用户名或电子邮件登录?

asp.net-identity asp.net-core

2
推荐指数
2
解决办法
5201
查看次数

如何在 Identity ASP.NET Core 2.1 (Identity Scaffolded) 中植入用户角色

当身份被构建时,如何为“管理员”用户角色设定种子?

我想通过电子邮件查找我的高级用户帐户并设置管理员角色。我发现的大多数示例都使用Startup.cs,但您应该使用它IdentityHostingStartup.cs来注册与身份相关的服务。

那么,/我如何注入RoleManagerUserManagerIdentityHostingStartup?(我假设这就是我需要的,请让我知道是否有更好的方法)

  public class IdentityHostingStartup : IHostingStartup
  {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDbContext<MyWebContext>(options =>
                    options.UseSqlServer(
                        context.Configuration.GetConnectionString("MyWebContextConnection")));

                services.AddIdentity<MyWebUser, MyWebRole>()
                    .AddRoles<MyWebRole>()
                    .AddRoleManager<RoleManager<MyWebRole>>()
                    .AddDefaultUI()
                    .AddEntityFrameworkStores<MyWebContext>();

                services.Configure<IdentityOptions>(options => {
                    options.Password.RequireNonAlphanumeric = false;
                    });
            });
        }
  }
Run Code Online (Sandbox Code Playgroud)

c# asp.net-identity asp.net-core

2
推荐指数
1
解决办法
2812
查看次数

如何检查是否在 Async C# MVC Core 方法中添加了实体项

我正在编写自己的 IUserStore 和 IUserPasswordStore 实现,我需要准备CreateAsync将返回IdentityResult. 到目前为止,我有这个:

    public async Task<IdentityResult> CreateAsync(User user)
    {
        Users.Add(user);
        //await Users.AddAsync(user);
        SaveChanges();
        //await SaveChangesAsync(user);
        //alternative version is commented

        if (<What to put there>)
        {
            return IdentityResult.Success;
        }
        return IdentityResult.Failed(new IdentityError { Description = $"Could not insert user {user.Email}." });
    }
Run Code Online (Sandbox Code Playgroud)

我真的不知道我应该在里面放什么if。我只能使用异步方法,所以当我运行这个函数时没有任何结果。有小费吗?我真的很感激。

通常我只是使用,Any()但这个实现不允许我。如果您需要更多代码,请在评论中提问,我会提供,但我认为这是足够的问题信息。

asynchronous async-await asp.net-identity entity-framework-core asp.net-core

2
推荐指数
1
解决办法
2007
查看次数

如何本地化 ASP.NET Core 2.1 Identity UserName 和 Password 错误消息?

我想为 asp.net core 2.1 项目以西班牙语提供身份用户名和密码错误消息的本地化字符串,因为它总是以英语显示消息。我按照http://www.ziyad.info/en/articles/20-Localizing_Identity_Error_Messages中的说明进行了尝试,但对我不起作用。

谢谢

asp.net-identity asp.net-core asp.net-core-2.1

2
推荐指数
1
解决办法
3008
查看次数

ASP.Net.Core 2.2 将 ID 从字符串更改为整数

我正在尝试将用户表的 ID 从字符串 (GUID) 更改为 int 并且真的很挣扎。我查看了很多示例,但它们似乎适用于 Identity 或 vs 的早期版本,并且由于多种原因它们不起作用。

我要么得到一个编译器错误

'Microsoft.AspNetCore.Identity.IdentityUser',在'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore 6[TUser,TContext,TKey,TUserClaim,TUserLogin,TUserToken]'上违反了'TUser'类型的约束。

或者当我创建迁移时,我仍然得到一个字符串列,而不是我期望的 int。

我正在使用 vs2019。Asp.Net.Core 2.2 和 Microsoft.AspNetCore.Identity 2.2

任何人都可以帮助我吗?谢谢!

entity-framework asp.net-identity asp.net-core visual-studio-2019

2
推荐指数
1
解决办法
2203
查看次数

Type or namespace SigninManager&lt;&gt; could not be found Identity 3.1.1 Mediatr 8.0.0

I am trying to implement Identity using the Mediatr library and pattern...

The code i am using did work in dotnetcore 2.x and identity 2.2 but is broken in dotnetcore 3.x and identity 3.1.1...

My Application class library is netstandard2.1 and hase the following dependencies set.

<PackageReference Include="FluentValidation.AspNetCore" Version="8.6.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.1" />
Run Code Online (Sandbox Code Playgroud)

I have my request handler like so;

public class Handler : IRequestHandler<Query, AppUser>
    {
            private readonly UserManager<AppUser> _userManager;
            private readonly SignInManager<AppUser> _signInManager; …
Run Code Online (Sandbox Code Playgroud)

asp.net-identity .net-core mediatr asp.net-core

2
推荐指数
1
解决办法
1115
查看次数

验证服务描述符 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory`1 时出错

在此处输入图片说明

首次尝试添加迁移时发生此错误。我已经添加了扩展方法和我的服务类

公共无效配置服务(IServiceCollection 服务){

        services.AddCors();
        services.AddControllers();
        
        services.Configure<AppSettings>(AppSettings);
        services.ConfigureJWT(Configuration);
        services.ConfigureIdentity();
        services.AddScoped<ILoginService, LoginService>();
        services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, CustomClaimPrincipalFactory>();
    }
Run Code Online (Sandbox Code Playgroud)

public static void ConfigureIdentity(这个IServiceCollection服务){

        var builder = services.AddIdentityCore<ApplicationUser>(o =>
        {
            o.Password.RequireDigit = true;
            o.Password.RequireLowercase = false;
            o.Password.RequireUppercase = false;
            o.Password.RequireNonAlphanumeric = false;
            o.Password.RequiredLength = 6;
            o.User.RequireUniqueEmail = true;
        });
        builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole),
       builder.Services);
        builder.AddEntityFrameworkStores<AuthDBContext>()
        .AddDefaultTokenProviders();


    }
Run Code Online (Sandbox Code Playgroud)

公共类 CustomClaimPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole> {

    public CustomClaimPrincipalFactory(
        UserManager<ApplicationUser> userManager,
        RoleManager<IdentityRole> roleManager,
        IOptions<IdentityOptions> optionsAccessor)
        : base(userManager, roleManager, optionsAccessor)
    {
    }

    protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
    {
        ClaimsIdentity identity …
Run Code Online (Sandbox Code Playgroud)

entity-framework dependency-injection claims-based-identity asp.net-identity asp.net-core

2
推荐指数
1
解决办法
4338
查看次数