自定义IPasswordHasher未被使用

时间:2019-09-27 14:42:49

标签: c# asp.net-core asp.net-identity core

因此,我尝试使用自定义的IPasswordHasher实现,但无论出于什么原因,似乎从未调用过该方法,但都有断点,但根本没有涉及到这些断点。

我的密码哈希器如下:

using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Identity;

namespace ViamondeMobile
{
    public class PasswordHasherMembership : IPasswordHasher<IdentityUser>
    {
        IPasswordHasher<IdentityUser> _identityPasswordHasher = new PasswordHasher<IdentityUser>();

        internal static string HashPasswordInOldFormat(string password)
        {
            var sha1 = new SHA1CryptoServiceProvider();
            var data = Encoding.ASCII.GetBytes(password);
            var sha1data = sha1.ComputeHash(data);

            return Convert.ToBase64String(sha1data);
        }

        //the password of the new users will be hashed with the new algorithm
        public string HashPassword(IdentityUser user, string password)
        {
            return _identityPasswordHasher.HashPassword(user, password);
        }

        public PasswordVerificationResult VerifyHashedPassword(IdentityUser user, string hashedPassword,
                                                               string providedPassword)
        {
            string pwdHash2 = HashPasswordInOldFormat(providedPassword);

            if (hashedPassword == pwdHash2)
            {
                return PasswordVerificationResult.Success;
            }
            else
            {
                return _identityPasswordHasher.VerifyHashedPassword(user, hashedPassword, providedPassword);
            }
        }
    }
}

我的Startup.cs ConfigureServices看起来像:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddJsonOptions(o =>
                {
                    o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddDbContext<ViamondemyviamondecomContext>(options =>

                options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("Viamonde"))
            );

            services.AddScoped<IPasswordHasher<IdentityUser>, PasswordHasherMembership>();

            services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<ViamondemyviamondecomContext>()
                .AddDefaultTokenProviders();


            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/build"; });
        }

但是对于我的一生,我不知道为什么不使用自定义哈希器,希望我遗漏了一些明显的东西,有人进来让我感到很傻哈哈!

编辑:

我用于测试的控制器是这样的:(不是世界上最科学的控制器,我只是想看到它在前进之前能起作用)

[Route("api/[controller]")]
    [ApiController]
    public class TripsController : ControllerBase
    {

        private readonly SignInManager<IdentityUser> _signInManager;
        private readonly UserManager<IdentityUser> _userManager;
        public ViamondemyviamondecomContext db = new ViamondemyviamondecomContext();

        public TripsController(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
        {
            _signInManager = signInManager;
            _userManager = userManager;
        }

        [HttpGet, Route("[action]")]
        public async Task<IActionResult> TestRegister()
        {
            var user = new IdentityUser
                       {
                           UserName = "mike",
                           Email = "ob@fuscat.ed"
                       };

            var result = await _userManager.CreateAsync(user, "9S5e2rQ8lKsy1595Y5^Ww#");

            return LocalRedirect("/");
        }

        [HttpGet, Route("[action]")]
        public async Task<IActionResult> TestLogin()
        {
            var result = await _signInManager.PasswordSignInAsync("mike", "9S5e2rQ8lKsy1595Y5^Ww#", true, false);


            if (result.Succeeded)
            {
                return LocalRedirect("/");
            }
            else
            {
                return LocalRedirect("/");
            }
        }

我只是调用TestRegister和TestLogin函数,然后查看我的Hasher上的断点是否命中,到目前为止,它们根本没有。

编辑:尽管我回答说链接的问题并没有真正帮助我,但我仍将其标记为重复,所以我想我把它放在这里,以防万一实际上被阅读,而你却永远不知道我想你好运。

基本上,我一直保持联系的问题无济于事,因为所有上下文现在都已经消失了,很难拼凑给出的答案应该是针对0上下文的特定情况,因此我再次询问。在问到这里之前,我遇到了这个问题,对我来说也无济于事。

0 个答案:

没有答案