ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects...

42
ASP.NET Core Identity Hans-Petter Halvorsen https://www.halvorsen.blog

Transcript of ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects...

Page 1: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

ASP.NET CoreIdentityHans-Petter Halvorsen

https://www.halvorsen.blog

Page 2: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

ASP.NET CoreIf you have never used ASP.NET Core, I suggest the following Videos:

• ASP.NET Core - Hello World https://youtu.be/lcQsWYgQXK4

• ASP.NET Core – Introductionhttps://youtu.be/zkOtiBcwo8s

ASP.NET Core Resources (Textbook, Videos, Code, etc.):https://halvorsen.blog/documents/programming/web/aspnet

2

Page 3: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

ASP.NET Core Identity• An API that supports user interface (UI) login

functionality out of the box.• You can manage users, passwords, roles, email

confirmation, 2FA, and more.• Users can create an account with the login

information stored in Identity or they can use an external login provider.

• Supported external login providers include Facebook, Google, Microsoft Account, and Twitter.

3

Page 4: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

ASP.NET Core Identity

• Introduction to Identity on ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity

• Scaffold Identity in ASP.NET Core projects: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity

4

Page 5: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Create Project in Visual Studio

with Identity Enabled

https://www.halvorsen.blog

Page 6: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Visual Studio

6

Page 7: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Select Authentication

7

Page 8: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Change Authentication

8

Page 9: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Solution Explorer

9

Lots of new stuff has been created for us that are used by the Identity features we have enabled

Page 10: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Create Identity Database

https://www.halvorsen.blog

Page 11: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

You need to create the Identity Database

11

Page 12: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Identity Database

12

Page 13: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Register New Account and Log In

https://www.halvorsen.blog

Page 14: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

You are now ready

• Create Account

• Log In

• Etc.

14

Page 15: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Register – Create New Account

15

Page 16: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Confirm Registration

16

Page 17: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Log In

17

Page 18: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

You are Logged In

18

Page 19: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Manage your Account

19

Page 20: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

2FA2 Factor Authentication

https://www.halvorsen.blog

Page 21: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

2 Factor Authentication• All modern systems offer what we call “2 Factor

Authentication”• This means in addition to enter the password, the

user needs to enter a one-time password received on E-Mail or SMS.

• An alternative is to use an Authenticator App like “Google Authenticator” or “Microsoft Authenticator” available on iPhone and Android.

21

Page 22: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Start using 2FA

22

Page 23: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Log In using 2FA

23

Enter Code from Authentication App

Page 24: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Application

https://www.halvorsen.blog

Page 25: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Start Creating your Application

• When the Identity features are installed, configured and set up, you can start creating the rest of your application

• Typically you need to check in your different web pages if the user is logged in (authenticated) or not

25

Page 26: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

@User.Identity

Typically you want to use the following:

• @User.Identity.IsAuthenticated

• @User.Identity.Name

26

Page 27: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Check if you are Logged In or not in your Code

27

You are NOT Logged In

Page 28: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Check if you are Logged In or not in your Code

28

You are Logged In

Page 29: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

@page@model IndexModel@{

ViewData["Title"] = "Home page";}

<div><h1>Welcome</h1>

<p>ASP.NET Core Identity Example Application</p>

<p>Is Authenticated: @User.Identity.IsAuthenticated</p>

<p>UserName: @User.Identity.Name</p>

</div>

Razor Code

29

Page 30: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

30

Page 31: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

…<div>

<h2>Data</h2>

@if (User.Identity.IsAuthenticated){

<p>Show Data here (e.g., from your Database)...</p>}else{

<p>You are not Logged In</p>}

</div>

Razor Code

31

Page 32: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Scaffolding

https://www.halvorsen.blog

Page 33: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Scaffolding• Scaffolding, also called scaffold or staging is a temporary

structure used to support a work crew and materials to aid in the construction, maintenance and repair of buildings, etc.

• https://en.wikipedia.org/wiki/Scaffolding

• Scaffolding, as used in computing, refers to one of two techniques: The first is a code generation technique related to database access in some model–view–controller frameworks; the second is a project generation technique supported by various tools.

• https://en.wikipedia.org/wiki/Scaffold_(programming) 33

Page 34: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Scaffold Identity in ASP.NET Core Projects

• Applications that include Identity can apply the scaffolder to selectively add the source code contained in the Identity Razor Class Library (RCL).

• You might want to generate source code so you can modify the code and change the behavior.

• For example, you could instruct the scaffolder to generate the code used in login or registration.

• Generated code takes precedence over the same code in the Identity RCL.

34

Page 35: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Scaffold Identity in ASP.NET Core Projects

35

If you are not happy with the default Layout of the different Identity web pages (Register, Login, etc.) You can override the default Scaffolding and modify these web

pages, so they fit your needs

Page 36: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Add Identity Scaffolded Item

36

Page 37: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Scaffold Identity in ASP.NET Core ProjectsSelect the Identity page(s)

you want to override

Page 38: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

38

Page 39: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Existing Login.cshtml

39

I want to remove this

Page 40: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Updated Login.cshtml

40

Page 41: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Resources

• Introduction to Identity on ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity

• Scaffold Identity in ASP.NET Core projects: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity

• Account confirmation and password recovery in ASP.NET Core:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm

41

Page 42: ASP.NET Core Identity - halvorsen.blog€¦ · Scaffold Identity in ASP.NET Core Projects •Applications that include Identity can apply the scaffolder to selectively add the source

Hans-Petter Halvorsen

University of South-Eastern Norway

www.usn.no

E-mail: [email protected]

Web: https://www.halvorsen.blog