ToProgramming Beta
-
Identity 2

Follow us on ToProgramming

Identity 2

How to work with identity 2 in MVC (Part 2)

Identity 2

Hello friends, I'm sorry, I did not work for a while, I was working on a new update of the site, which will be uploaded soon

Let's move on to the main discussion

We went through the web config and it was over

So let's start the program and see what it has

Now go to the login page...

We can with
Username: admin@example.com
Password: admin@123456
Enter the admin panel

Well, let me explain some of the code to you

At the project location, go to the app_start folder and open IdentityConfig.cs

I will explain the parts that I know to you

<hr>            

manager.PasswordValidator = new PasswordValidator

            {

                RequiredLength = 6,

                RequireNonLetterOrDigit = true,

                RequireDigit = true,

                RequireLowercase = true,

                RequireUppercase = true,

            };

<hr>

In line 36

RequiredLength This line specifies the minimum number of passwords

RequireDigit Letters like @

RequireLowercase , RequireUppercase Controls uppercase and lowercase letters

Look at line 114

Yes, this part builds your admin panel

So let's go to Startup.Auth.cs

 // //app.UseMicrosoftAccountAuthentication(

            //    clientId: "",

            //    clientSecret: "");

            //app.UseTwitterAuthentication(

            //   consumerKey: "",

            //   consumerSecret: "");

            //app.UseFacebookAuthentication(

            //   appId: "",

            //   appSecret: "");

            //app.UseGoogleAuthentication(

            //    clientId: "",

            //    clientSecret: "");

I have nothing to do with other sections, but I have a lot to do with this section that I wrote for you
It is related to external login

I will learn how to work with these later

Because it's a lot of work

Well, friends, sometimes we want to add a table to the identity table...

To do this go to the Models folder and to IdentityModels.cs

Friends, identity models are made here

Well, you should create a property there

Under public class ApplicationUser: IdentityUser

    public class ApplicationUser : IdentityUser

    {

<here>

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)

        {

            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType

            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            // Add custom user claims here

            return userIdentity;

        }

    }

Write this code
public TYPE Name {get; set; }

for example

 public class ApplicationUser : IdentityUser

    {

        public string FirstName { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)

        {

            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType

            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            // Add custom user claims here

            return userIdentity;

        }

    }

Well, the table was made in Identity, now let's get it from the user

I will also create a LastName

I want to get these from the user in the Register form

Enter AccountViewModels.cs in the same folder

And then go to RegisterViewModel in AccountViewModels.cs

And build a model
for example
         [Required]
         [Display (Name = "Firstname")]
         public string Firstname {get; set; }

         [Required]
         [Display (Name = "Lastname")]
         public string Lastname {get; set; }

And now we go to the Register form

In the View next ŮŽ Account

Open the registry where the codes are

Copy one of the codes (I suggest we copy the email)

for example

@Html.ValidationSummary("", new { @class = "text-danger" })

    <div class="form-group">

        @Html.LabelFor(m => m.Firstname, new { @class = "col-md-2 control-label" })

        <div class="col-md-10">

            @Html.TextBoxFor(m => m.Firstname, new { @class = "form-control" })

        </div>

    </div>

    @Html.ValidationSummary("", new { @class = "text-danger" })

    <div class="form-group">

        @Html.LabelFor(m => m.Lastname, new { @class = "col-md-2 control-label" })

        <div class="col-md-10">

            @Html.TextBoxFor(m => m.Lastname, new { @class = "form-control" })

        </div>

    </div>

How to work with identity 2 in MVC (Part 2)

8/23/2020 7:23:02 PM
ToProgramming
0
95
Asp.Net

You leave the first comment


Post Comment

You must be authenticated to submit a comment