0

In my application i want just user login without registration,to disable registration i was following this article.

To add new user into database i use sql:

INSERT INTO AspNetUsers
(
[UserName],
[Email],
[EmailConfirmed],
[PasswordHash],
[AccessFailedCount],
[Id],
[PhoneNumberConfirmed],
[TwoFactorEnabled],
[LockoutEnabled]
)VALUES
(
'Uladz',
'test@gmailcom',
'true',
'adm1nPassWorD*_',
'3',
'1',
'false',
'false',
'true'
)

but i have problem with [PasswordHash] insert it as regular string makes no sense it has to be encrypted by identity.

Could you please tell me how can i get it or i have to use another way to do it ?

1 Answer 1

2

You could try Data seeding to set the user data.

1.add a new class called MyIdentityDataSeeder

 public static class MyIdentityDataSeeder
    {
        public static void SeedData(UserManager<IdentityUser> userManager)
        {
            SeedUsers(userManager); 
        }

        public static void SeedUsers(UserManager<IdentityUser> userManager)
        {
            if (userManager.FindByNameAsync("Uladz").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.UserName = "Uladz";
                user.Email = "test@gmailcom";
                user.EmailConfirmed = true;
                user.AccessFailedCount = 3;
                user.Id = "1";
                user.PhoneNumberConfirmed = false;
                user.TwoFactorEnabled = false;
                user.LockoutEnabled = true;

                IdentityResult result = userManager.CreateAsync(user, "adm1nPassWorD*_").Result;

            }
        }
    }

2.modify your Configure() method signature as shown below

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager<IdentityUser> userManager)
    {
        ...
        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();


        MyIdentityDataSeeder.SeedData(userManager);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
    }

Reference: http://www.bipinjoshi.net/articles/5e180dfa-4438-45d8-ac78-c7cc11735791.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, i think "seed" way more useful than "db".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.