Mastering Custom Cookie Authentication : A Guide to .NET 8 Blazor with Interactive Wasm Render Mode.

Netcode-Hub
28 Feb 202453:19

Summary

TLDRこのビデオでは、.NET 8 Blazor Webアプリケーションでのカスタムクッキー認証の実装について詳しく解説しています。講師のフレデリックは、WebAssemblyレンダーモードでのクッキー認証の作成方法をステップバイステップで説明し、サーバー側とクライアント側の両方で必要な設定とコードの変更を紹介します。具体的には、パッケージのインストール、データベースサービスの設定、新しいプロジェクトの作成、ログインと登録フォームの実装などが含まれます。このビデオは、.NET技術、特にBlazor WebAssemblyに関心のある開発者にとって非常に有益な内容となっています。

Takeaways

  • 👨‍💻 Demonstrates how to implement custom cookie authentication in Blazor WebAssembly app
  • 🔑 Uses Identity framework to handle user authentication and authorization
  • 📝 Creates custom ApplicationUser class to extend IdentityUser
  • 📥 PersistentServerAuthenticationStateProvider prepares auth state on server
  • 📤 PersistentAuthenticationStateProvider maintains auth state on client
  • 🌐 Minimal API endpoint used for logout to access HttpContext
  • 📃 Customizes login and register pages
  • 🍪 Shows how auth state and cookies change on login/logout
  • ⚙️ Registers services and components needed for custom authentication
  • 🔐 Implements authorization check on Razor components using @attribute

Q & A

  • Blazor WebAssemblyのレンダーモードでカスタムCookie認証を実装する際の主なステップは何ですか?

    -主なステップには、クライアントとサーバー側のパッケージのインストール、データベース接続文字列の作成、データベースサービスの追加、Identity DBコンテキストの設定、PersistentServerAuthenticationStateProviderとPersistentAuthenticationStateProviderクラスの作成、ログインと登録ページの作成が含まれます。

  • BlazorアプリケーションでカスタムCookie認証を実装する目的は何ですか?

    -カスタムCookie認証を実装する目的は、デフォルトの認証メカニズムをカスタマイズして、アプリケーションに合わせた認証フローを提供することです。これにより、特定のユーザー認証要件に合わせて認証プロセスを細かく制御できます。

  • 「PersistentServerAuthenticationStateProvider」と「PersistentAuthenticationStateProvider」クラスの役割は何ですか?

    -「PersistentServerAuthenticationStateProvider」クラスは、サーバーサイドレンダリング時にユーザー認証を処理します。一方、「PersistentAuthenticationStateProvider」クラスは、クライアントサイドで認証状態を維持し、利用します。

  • Blazor WebAssemblyプロジェクトでIdentityを設定する際に必要なパッケージは何ですか?

    -EF Core、SQL Server、EF Tools、Microsoft.AspNetCore.Components.WebAssembly.Authenticationなどのパッケージが必要です。

  • カスタム認証でユーザー情報をDBに保存するためにIdentity DBコンテキストに追加する必要があるクラスは何ですか?

    -アプリケーションのユーザー情報を保存するために「AppUser」クラスをIdentity DBコンテキストに追加する必要があります。これにより、デフォルトのIdentityユーザークラスを拡張できます。

  • Blazorアプリケーションでカスタム認証のために必要なデータベースの設定はどのように行いますか?

    -appsettings.jsonファイルに接続文字列を定義し、Program.csファイルでこの接続文字列を使用してEF Coreデータベースコンテキストをサービスに登録します。

  • ログインと登録ページをBlazorアプリケーションに追加する際の注意点は何ですか?

    -ログインと登録ページには、適切なルート属性を設定し、ユーザー入力を受け取るためのフォームを含める必要があります。また、認証処理を実行するためのロジックも実装する必要があります。

  • Blazor WebAssemblyアプリケーションでログアウト機能を実装するためにはどうすればよいですか?

    -

Outlines

00:00

😀 Introducing the video and goals

The speaker introduces himself and states the goal is to look at custom cookie authentication in Blazor using interactive web assembly.

05:02

😀 Installing necessary packages

The speaker walks through installing Entity Framework, SQL Server and other necessary NuGet packages.

10:09

😀 Setting up the database context

The speaker sets up the database context by creating an AppUser class that inherits from IdentityUser and an AppDbContext class.

15:09

😀 Implementing user authentication

The speaker implements authentication by creating classes to manage the authentication state on the server and client side.

20:16

😀 Building the login and register pages

The speaker builds out the login and register pages, including the necessary models, methods and UI.

25:17

😀 Adding the ability to log out

The speaker adds the ability to log out by implementing a minimal API endpoint and wiring it up correctly.

30:22

😀 Accessing user claims

The speaker accesses the user claims to display the user's name instead of their email on the nav bar after login.

35:27

😀 Wrapping up

The speaker concludes by summarizing the video and key things that were covered regarding custom cookie authentication.

Mindmap

Keywords

💡custom authentication

The main theme of the video is implementing a customized authentication system in a Blazor web app using cookies. This allows controlling how users log in and access authorized content.

💡interactive web assembly

The video focuses on adding custom authentication in Blazor apps using client-side web assembly rendering specifically. This renders UI components client-side.

💡authentication state provider

A class that handles managing the user's authentication state across the app. The custom server and client implementations allow sharing auth state.

💡identity services

Services like Identity DB context, user manager, sign in manager etc. that are used to implement core user identity and access functionality.

💡claims

Properties like user ID, email etc. that are extracted from the authentication state to identify the user.

💡authentication token

The token string stored in the browser cookie that contains information about the authenticated user.

💡cascading authentication state

A service that propogates the user's authentication state across the Blazor app for authorized components.

💡custom cookies

Instead of the default identity cookies, custom tokens are generated after user login and stored to authenticate them.

💡login / logout

Custom login and logout pages are created to demonstrate usage of the custom authentication implementation.

💡navigation manager

Used to navigate between pages in the Blazor app, utilized for redirects based on authentication state.

Highlights

Explains custom cookie authentication in Blazor web assembly interactive mode

Installs packages and sets up database services and identity context

Creates custom application user model

Sets up persistent authentication state providers

Implements login, register and logout pages

Secures routes with authorization

Registers services for authentication

Shows demo of register and login functionality

Displays logged in user's name

Tests logout and removes authentication cookie

Creates multiple test users

Fixes display name issue from database

Rebuilds and retests application

Verifies customized name display

Concludes overview of implementation

Transcripts

play00:00

hey everyone welcome back to net code help  Channel I am Frederick and I'm happy to have  

play00:05

you here today in this video we are going to  look at custom cookie authentication in net8  

play00:12

Blazer web app with interactive web assembly  render mode I've made a video on how to create  

play00:19

cookie authentication in interactive server  render mode and on this video is focusing  

play00:25

on the web assembly render mode when we say  cookie it is just a piece of um token which  

play00:34

is set to the cookie storage of a browser  and now which holds the identity of the user  

play00:40

anytime a request is being made the token  or the cookie has to be retrieved by the  

play00:44

server verify it and um grind access to the  user and now in this case we have a default  

play00:51

one um that is when you use the individual  account and this time around to we are going  

play00:58

to look at how to customize that into a small  unit that you can implement it easily in your

play01:03

application so before we Dive Right into this  make sure you have subscribed already and also  

play01:11

click on the notification Bell to receive update  as soon as I do upload new video like this also  

play01:18

like this video to support this Channel and this  video as well the source code of this project  

play01:24

will be available as you know already so if you  want to grab it to just guide you check the video  

play01:29

description and it will be available if you want  to have a look on the authentication the custom  

play01:35

cookie authentication using the interactive server  render mode then check the playlist it's all found  

play01:41

under the description so you can actually check it  over there I do offer coaching session to people  

play01:47

who are interested in net Technologies talking  about Blazer web assembly server net8 um web  

play01:55

API Mar Blazer hybrid and a lot so if you're  interested check the video description I have  

play02:02

an email in there you can write to to me through  it all right so let's Dive Right into this we're  

play02:09

going to create a new project so launch of Visual  Studio 2022 and I click on new project I know by  

play02:17

now you know how to create project isn't it  yes of course so click on Blazer web app and  

play02:22

here we're going to give the name as custom cookie  Earth Blazer we assembly what I'm trying to mean  

play02:29

here C some Quicky authentication in Blazer web  app with web assembly interactive um render mode  

play02:37

so choose a location here and I click on next to  get this project created so once the project has  

play02:45

finished creating what we're going to do here  is we're going to install our packages for the  

play02:51

client and also the server aside from installing  the packages we are going to create our connection  

play02:58

strings and now add database services and Etc  to get at the database of identity DB contest  

play03:05

because we're going to use identity manager  in this so we have to add the services and  

play03:11

un perform migration good and I believe for that  you know how to do it already isn't it now when  

play03:16

we are done we going to add idty DB contest this  is not a um connection for database rather for  

play03:25

user manager okay you're going to add a service  so wait after adding the services we're going to  

play03:30

create two classes one in the server and one in  the client the server one you're going to name it  

play03:35

as persistent server authentication State provider  and now the client one is going to be persistent  

play03:41

authentication State provider why are you creating  these two classes forther research the persistent  

play03:48

server authentication State provider prepares  authentication of the user in the server that is  

play03:54

when server side is rendering that is server  side rendering and now it's sent it to the  

play03:58

client now when it gets a client the state of the  authentication must be maintained and it must be  

play04:04

utilized as well and that is where the persistent  authentication State provider comes in so it makes  

play04:10

sure that authentication which has been received  from the server is intact and it then utilizes  

play04:16

in the client side rendering you see so we need  these two classes one for each of the projects  

play04:22

okay once you're done we create our Pages log in  and now register and now off you go so these are  

play04:31

the stages are going to pass through to finish  this cookie or custom cookie authentication with  

play04:37

interactive web assembly render mode now you have  the gist of it let's get going now when you go to  

play04:44

the solution you see we have two projects in here  we have the client this one and that is the server  

play04:50

so that's where we're going to create our custom  modication to we focus on the uh the server then  

play04:56

we're going to consume it in the client very  simple one okay so all the work are going to  

play05:01

happen in the client not in the server and when  you are done the client is just going to consume  

play05:07

it very simple way okay now let's install packages  in here and you know since you're going to use um  

play05:14

idb context it means we need a ef ef tools and now  SQL Server so right click on dependencies and now  

play05:22

let's add this packages so click on the browse Tab  and in here we going to search for the efcom tools  

play05:31

um the tools and that's again we have toq server  and the efq itself okay so here that's what we  

play05:39

have so for me since I have the package reference  already I'll just close this then I I click on the  

play05:46

server site I go to unload project and now from  this section the item group I'm going to paste it  

play05:55

in here so what I need to do here is just type in  an item group and I'll paste this here this is the  

play06:03

one that I needs okay so I'll add them in here so  we need three packages EF call SQL server and our  

play06:10

tools okay let's save this right click solution  right click this and now reload project with

play06:20

dependencies we then go to the client you right  click on the client then go to unload project and  

play06:29

I'm going to add the same thing to this that is  a component web assembly authentication so this  

play06:35

what we need then let's save this as well and  now go to reload project with dependencies so  

play06:44

when you check dependencies packages you can  see we have the packages in here right when  

play06:49

you check for the dependencies packages for  the client we also have then here what left  

play06:55

here is to build this together so let's build  solution all right so you can see that the the  

play07:01

project building is completed and our two  suceeded it means that all the project are  

play07:06

built the next thing that we're going to do  here is to create our application user now  

play07:13

we can defaultly when to use identity user  it has a default buildin properties and here  

play07:22

maybe you like to add some properties to this  so maybe you're going to extend the class and  

play07:27

we going to add maybe username okay so we  have to do that let's come to and this must  

play07:37

be held in the server project so take note  we let's add maybe class a folder here and  

play07:46

I'm going to say this of authentication so  everything here is of authentication right  

play07:51

click on this you're going to add a class to  and I'm going to name it as AB user for short

play08:01

this app user is going to inherit from Identity

play08:03

user identity user and now this is coming

play08:11

from let's see so Microsoft as  net code identity so this has  

play08:22

some default properties and we want to add name to

play08:26

this so if case you have any  extra properties you just add

play08:34

it okay so this is what we need to  do now aside from doing this let's  

play08:43

now go in and create our appdb contest so in  authentication I'm going to add my abdb contest

play08:49

class and now this class is  going to H from Identity DB

play08:59

contest and now within this you  have to pass in the app user that  

play09:08

we created we can create Constructor for  this then we pass in this appdb contest

play09:19

class okay you need not to create any table  here okay because this has it all that's all  

play09:30

that we need to do so let's save this the  next thing that we're going to do is to  

play09:36

create our connection string in the app  setting. Json so here we're going to say  

play09:42

that connection strings then you're going  to be default connection so I like to name  

play09:48

it as only default now server is equal to  local and database this equal to wasm ah  

play10:01

so I'm going to say cookie DB now aside  from this I'm going to say trust server

play10:08

certificate or this is trusted

play10:13

connection set this to  through and now trust server

play10:20

certificate let's set this to through as well  okay so this what that we need to do let's save  

play10:31

this the next to do is register the connection  string in the program.cs file so go to the next  

play10:38

stage and then before the app build you're going  to say builder. services. rdb contest we pass in  

play10:47

this appdb contest class that we created  in that we're going to pass in an option  

play10:54

an option. use SQL server and in here we have  to provide the class connection string that we  

play11:03

created so get connection string and that is this  one and what is the name do you remember that is

play11:11

default so default you must make sure it is the

play11:18

same okay so default can just cross check it copy  this go to app settings and I make sure it is the  

play11:33

same that's fine okay so now that we are done we  need to register the identity manager service so  

play11:41

down here we're going to say builder. services  do add identity call this one passing the app

play11:52

user so here Dot add Entity framework store  and the store will need an application appdb  

play12:05

contest class aside from this you passing add  signning manager and the last one add default  

play12:18

token providers okay so we need to add this servic  in here okay now that we are done we can go ahead  

play12:28

and app for my migration so let's save this  we're going to build everything again so build

play12:35

solution okay so now that this is built  we going to perform migration we go to  

play12:43

package manager console and in here  we're going to so add- migration we  

play12:52

want to put this so migration  and I'm going to say this is

play12:55

first now after this has been  created we're going to update

play13:03

database now this is going to create all tables  that are necessary for this Identity or user  

play13:15

manager okay so you can see we have a lot of  tables being created when you check here you  

play13:20

know this already so I need not to explain but  if you don't know these are the tables that is  

play13:25

going to create and these are the columns I'm  going to create for each and all these are  

play13:29

coming from this this guy this one okay good  okay so we have the table we have a database  

play13:37

created if you want to actually check it click  on SQL Server object Explorer then in here you  

play13:43

have to check the service my server here is  a local so I'm going for this SQL service  

play13:48

and that's the last one desktop so go to  database and you can see cookie DB cookb  

play13:55

this the database and if I open the tables you can  see that you're going to have a lot of tables in

play14:00

here so let's wait for the table to get loaded you  can see you have all this table coming out from  

play14:08

theid DB contest class that's fine okay okay so  this even the cookie DB so when you check cookie  

play14:16

DB it is the same okay so tables let's close this  yeah can see we have the same thing in here so now  

play14:26

that we are done we're going to now prepare user  authentication okay so in that we have to create  

play14:32

our persistent Ser authentication State provider  as I said earlier on this going to inherit from  

play14:38

server authentication State provider and now  this prepares the user authentication using the  

play14:45

persistent component State service okay then aside  from that to repace it send to the client and I  

play14:52

in the client persistent authentication State  provider also which in from the authentication  

play14:57

State provider receives it and I'll maintain  it and utilize it in the client session when  

play15:02

the app is running in an um CS the client side  rendering but in the server side rendering as  

play15:09

SSR it is the persistent server authentication  State provider so let's go in then and create that

play15:15

class so from the authentication we're going to  

play15:21

add a new class to this and I'm  going to say that is persistent

play15:29

server authentication States provider okay  so that is a name very long persistent server  

play15:42

authentication State provider okay so we  have this and now this class is inheriting  

play15:49

from server so server do you have it  let's see Server Authentication State  

play15:59

provider okay and in that you have  to implement the interface for ID

play16:04

disposable then you know we need to implement  something like this and interface in here okay  

play16:15

that's a dispos method to get rid of that that  error okay so in here what I'm going to do  

play16:22

let's have our um private read only property  for persistent component States so I'll this

play16:29

this so that'll be this one  and aside from that you have  

play16:36

this ping component State we also have to get

play16:39

our identity options so let's also get this  property also here the next one we need our  

play16:50

component State subscription so we need to get  this guy so this are all property is that we  

play16:59

are setting and and the next one we need our  authentication task so let's also have this  

play17:06

one so we have priate only because aside from  this scope we don't want to be using this you  

play17:11

want to maintain it to the scope of this okay  so you know we have to generate a Constructor  

play17:17

and we need to register so from here we create  our constractor and this is a Constructor so  

play17:26

we pass in this persent components State and now  we're initializing it in here so state is equal  

play17:31

to this person component state so we initialize  it in here okay now authentication State change  

play17:37

we want to call this method we haven't called any  method yet so we're going to write defination for  

play17:43

this method okay when authentication changes  that's where you want to get this guy called  

play17:49

okay so on authentication State change we want  to just assign the authentication task to the  

play17:57

current task that we have and that exactly what  we doing from the next one so on authentication  

play18:02

State change you passing it's going to call  or it's going to pass in this as a payload  

play18:07

and now we assign the property that we created  in here to the outcome or the results of this  

play18:13

task okay good now we are done we go to the next  one that's the UNP persistent async so in this  

play18:22

method what we're going to do here is we're  going to extract because when the page loads  

play18:26

when the page refreshes the component state  is going to be sent from here or we're going  

play18:32

to prepare the the user authentication and I'll  send it to the client okay so we need to pass  

play18:40

in properties that we need such as our claims  and Etc and the claim you could use an ID name  

play18:48

email and Etc so that's where you need to Pro  provide or prepare all these um materials okay  

play18:55

that's what we're going to be using so here with  un persistent async what are we doing we first  

play19:05

want to assign or get the authentication of the  user and that's what we doing here so we assign  

play19:12

this authentication State provider to the tax  that we have been assigned it in here and aside  

play19:19

from that we want to grab the principal of that  user so we can get it from this authentication  

play19:23

State and quickly let's grab that okay so render  mode we need to include this one so we get the  

play19:32

user so now when we get the user we can now go in  and extract the user info so we going to extract  

play19:39

it let's first check if is authenticated that is  where you want to do this because if it has not  

play19:47

authenticated it means you don't have any current  claims and you cannot extract claims once they  

play19:53

are not there okay so we want to make sure they  are there and here we need need maybe the user

play19:59

ID you need the user ID in here  we also need user email and maybe  

play20:09

we're going to need a name so we  can add one now let's say this is

play20:15

name so we can have an extension of name username  okay so do we have username we have um username in  

play20:29

here so maybe since we are not having that uh we  can use we can just get the name directly from the  

play20:35

claims which is coming from the principal and we  can just search for the name that we added and I  

play20:41

get the value okay so we have these three claims  and that's what we're going to be using okay but  

play20:47

here in order to send this three properties to  the client we need to have a class that get to  

play20:54

handle all this because it is appropriate to  put everything in a model so we can just send  

play21:00

it once and for all okay so in that we need to  create a class and this class you can name it as  

play21:06

user info and Etc so maybe we can just go to the  client and now since you're going to consume this  

play21:12

in the client we need to put it in a client so we  can be using or you can add Modo to this and I'll  

play21:17

add it in here but don't worry you can actually  add it from this so with this two we're going to  

play21:23

say this an authentication from the client and we  going to to um add a class this class is going to  

play21:33

be an user info and that user info we definitely  going to use user ID this user ID you know it is  

play21:43

supposed to be string and we need an email for  this and email you know is also what string and  

play21:52

you can add last one and that's going to be name  okay so so we need these three properties and now  

play22:00

in here we can just go in there and now check  so before we checking we can before we assign  

play22:07

this or we populating this to the model we can  just check it first to see if the claim that it  

play22:12

got none of them is null before we start working  on it okay so if user ID to null and let's add  

play22:21

this let's add the name as well because we added  that so and so this this time around is not going  

play22:29

to be name a email it's going to be what name  so if they are not now that is only when you  

play22:38

want to prepare this from the component state so  let's say we want to prepare them here so we grab  

play22:45

them and as the user info and now here you can  see from this name space we are adding it from

play22:51

here and that is from the client  Authentication folder okay we are  

play22:59

not using Row for now so we change this to  name and now we are making a reference of this

play23:05

name if it is row that you want to also  add then you add it to you add it as a  

play23:13

row and that's all okay so now that this is  set and done when the page disposes we want  

play23:20

to dispose a subscription and also you want to  uh remove the authentication State change yes  

play23:28

so this is all we need to do very simple one as  well and that is it one okay so let's save this  

play23:35

then we can now Clos this after creating this  you know definitely you have to register the  

play23:42

service isn't it yeah so we go to the solution  program and now in here we can add the service  

play23:52

so br. services. add let's first add cascading  authentication state before we forget it again  

play24:00

and let's add beta. Services dot this going  to be scoop and I we need our what is the  

play24:09

name we have persistent authentication  State provider and now this is coming

play24:15

from you know this supposed to be here supposed  to be provider because it is coming from this okay  

play24:25

so I it provider then press assistant allate  provider so we are creating a scope depend  

play24:35

injection between this provider the main one and  there's the fake one okay so that's what we need  

play24:43

to do in here okay so aside from that we go to  our route and Route can be find the component  

play24:49

folder and in a route. Razer we never going to  find it in here okay so inside this route we  

play24:58

need to make some few changes in here okay so  in here I'm going to just replace this one and  

play25:06

um we have main. layout so you know where it's  coming from we have to include the name space in  

play25:12

here using this authorization and now with this  redir login we're going to create this later on  

play25:16

so for now we can comment this up okay so it is  is a noral thing that we doing all that I did was  

play25:24

to add this authorized Rod View and now if it is  not you want to redir to login okay that is all  

play25:30

thing all I did and now here we need not to at  first when you check Net 7 net say and Below you  

play25:36

have to add cascad authentication State markup or  the component to S this isn't it good but for now  

play25:44

since it has turned to a service and you have  added a service then I don't think you need to  

play25:49

use that so you can skip it if you decide to also  bring it well it will be there but if you escape  

play25:56

it it's going to work so the next thing that we're  going to do here is um you're going to work on the  

play26:02

sign out now when using identity manager if you  want to sign out you must sign out in an HTTP  

play26:09

contest and now this happens only when a call is  being made to an API that's where you can have  

play26:15

access to this else when you decide to use an HTTP  contest as a cascading um value and you want to  

play26:23

sign out it's not going to work okay because that  instance answer is only read only if you want to  

play26:32

if you can have access to this to sign out then  we have to use a Minima API okay so we're going  

play26:39

to create one Minima API and that's I'm going to  use to sign out if you actually check the original  

play26:46

docs or the original implementation of this when  you choose individual account you will see that  

play26:52

the same thing is what you're going to do here  okay so no different the same thing just that  

play26:57

going to scrap all the one that we don't need and  I maintain the only ones that we need and that's  

play27:02

only one here one for sign out okay so this going  to happen in the server we are still you see that  

play27:10

we are still working in the server we haven't  touched the client yet so we are still working  

play27:15

in here okay so let's right click on this and the  name is too long for me so let me just copy this  

play27:22

from here so we click on the authentication we're  going to add a class to this and this is going  

play27:29

to be identity component end point route Builder  extension H very long okay so the reason why the  

play27:36

name is too long here is when you go to program.cs  it is coming from this Builder extension okay so  

play27:43

we going to implement as a part of this and  it's also idty is an end point so that's why  

play27:49

they combine it but well you can just modify this  um to make it a small one for yourself okay so now  

play27:58

let's go back now we have this this must be an  internal and it must be a static class so we're  

play28:06

going to use it in here only okay now within this  we need to create a static method which at the  

play28:17

end is going to return our end point conversion  Builder so let's say it's going to return our  

play28:24

endpoint convention Builder and now this is a  name map additional entity end point and now  

play28:29

within this we can pass in the I end point right  Bel that a parameter and now the extent that going  

play28:36

toate going to affect this so anytime or wherever  we are we can grab the contest from this end point  

play28:43

throughout Builder straightforward okay so maybe  would it be possible if I bring it here okay then  

play28:53

let's see so now that we have this what are you  going to do very simple endpoint we're going to  

play28:59

check if it is now we can try an exception or we  can skip this I don't think this is necessary for  

play29:04

now okay so let's create our account group we map  the group in here so if you want to check this we  

play29:13

create an account group and now the account group  the route here is an account SL account and we  

play29:18

have a method known as log out so the full roue  going to be account SL logout okay we inject our  

play29:24

principal then our signing manager which coming  from this app user not application you come  

play29:30

from the app user and then here we have a signing  manager because that's the instance of the signing  

play29:35

manager and I know this manager can assist us in  signing in and I'm signing out so while it gets  

play29:41

up to this with a context we can just rather sign  out in here when we sign out you want to redirect  

play29:47

to the homepage so you can specify if you want to  redirect to any page of your choice you can just  

play29:51

pass in maybe lock out then this and that but  as soon as you sign out you go to the home home  

play29:57

page okay that's what we are doing for now okay  now let's save this so we are done with this now  

play30:02

what we need to do is to register this so where  can we do it what do you think H definitely going  

play30:07

to do the service what the service the program  so go to program.cs file and now where we have  

play30:14

this um pipelines we're going to pipe it line  in here so let say app. map identity the this  

play30:22

one okay we have to map it in this format very  simple one and and that is all okay there comes  

play30:30

in our form so we need to create our login and  our register forms right click on the pages and  

play30:37

let's add maybe we can add a folder here and name  it as authentication or we can keep going and add  

play30:43

a page let's make this as login so in here on  top we need to inject our signing manager user  

play30:54

manager and also navigation manager you need to  inject this aside from that you can specify log to  

play31:01

this so maybe account SL login or just login only  there's a route login okay now this must come from  

play31:11

microsoft. notore Identity so we have to include  the name space in here okay let's save this and  

play31:22

now this must be an app user not application so  app user and you know it must we must add the  

play31:31

reference so we must include this reference  in here okay now we have to design our form  

play31:40

but before we design our form we have to create a  model and maybe this is going to be a model we're  

play31:46

going to have we're going to maintain what we have  already default one and down here we're going to  

play31:51

have our input model so this is a seed class and  we have you have to include this data annotation  

play31:58

and we have email and password okay so we don't  want to remember me for now we want to have email  

play32:05

and password okay so once you have this we going  ahead to create an instance of this so you want  

play32:15

to say that from here we have form input model  as input then we create a new now if I click on  

play32:25

or if I fill up the model and I click on login  this what I want to execute I want to just log  

play32:36

in log in the user so let's have this method  in here very simple one so log in a sync and  

play32:45

now if I log in user this what I'm doing okay so  we have this public and I think we have to close

play32:52

this yes and and for this error I think I have to  you have to create a property to handle this on  

play33:03

top here string error and now you have it set okay  now claim times we need to include system. claims  

play33:11

to get this also solved now can see from here  that we find the user by the email that has been  

play33:16

entered if we find it uh we're going to get here  we don't need to get rows here because you're not  

play33:22

adding rows for now so in case you add rows then  you have to get the rows and also add it in here

play33:29

so for now let me just remove the rules okay you  want to just have only authenication for now so  

play33:36

we have um claims I have a list of claims and  we have a name and email okay we have user ID  

play33:45

you can also add that then here we can sign  in manager you can check the passw if they  

play33:52

correspond then we want to now sign in and I will  tell me the errors to what success or this going  

play33:57

to be a message or anything else that you want  to or you can just remove this in case you are  

play34:02

in this what you're doing so we're going to  password signning and that's provide the the  

play34:10

name that's the app user then provide a password  and now here we are checking if if I click on come  

play34:18

here tells you the next step is it persistent  then it's a lock out on failure so it's no we  

play34:23

it for f and that's what that we need to do in  here very simple one okay now let's have our  

play34:31

model created or there is a form an edit form  we can create an edit form for this and I am  

play34:37

using the default login so this is a default  login we have the the usern name we have the  

play34:44

password and that is it I want to remove this  remember me cuz I don't want to integrate this

play34:50

now okay then we have our button so edit  form you have the model and the method  

play34:59

here it is post we have form name in here we  provide the method and now the form name for  

play35:06

it to because this this is going to render in  static way so this a static rendering and um  

play35:12

if you don't not improve provide this it's  not going to work okay so we have to or if  

play35:17

you take out this one then you have to include  interactive um render mode as what for assembly  

play35:27

okay so that's what we doing and here we're  displaying an error to the user all right now  

play35:33

this is all that we have we have to work on the  registration tool so let's create a registration

play35:40

component the same Pages I'm going to right  click on this and create a component known as

play35:48

register so with this two we we need  our user manager so this what we can  

play35:58

do we can just move the ones that we have  inside this the login to um import rer or  

play36:06

we can still repeat this the best way okay  so we should not repeat this because when  

play36:11

you check the login we have it already so  we can just um grab it from here see the

play36:19

register

play36:23

okay so down here we need to also create our  model for loog registration so we have name  

play36:32

email password and a confirm password okay  now aside from having this in case I click on  

play36:39

register what do I want to do first let's create  um an object out of this after the model so input  

play36:46

model for the register and now in if I click on  register what am I going to do we want to execute  

play36:54

this we first have to create an instance of this  app user and now pass in populate it so that's  

play37:02

what we are doing we ass signing to this and we  can now keep moving so here must be an app user  

play37:07

as you know that is a model aside from doing  that we're going to create user asnc so from  

play37:14

here once you have everything created because this  password has is going to Hash the password of the  

play37:20

user automatically and we can now create the user  account if it is access or in case it is not what  

play37:27

you want to do you can just retrieve it but let's  assume it is Success then that is all okay so you  

play37:35

see this is very simple so register user and now  create a user very simple one okay now let's save  

play37:43

this so we have our login we have our register now  we have sign out so now let's go to the nav menu  

play37:55

and this can be found in the shared or the layout  you see we have a nav menu in here and with this  

play38:00

we want to replace this one with this because here  we have this authorized attributes surrounded with  

play38:08

and this we need to include the name space okay  so at using this authorized view need to include  

play38:15

them here so what we're doing here is now when  need check this authorized view okay now need  

play38:23

check this we want to this is an au requir if I  click on this but for now you want to remove this  

play38:29

you didn't want that one so you want to display  only the name okay so you can display this when  

play38:36

you when you log in you want to display the name  that's all and also here this the most important  

play38:44

thing that's for the log out you know we is a  form this is a PHP form so can see we there's  

play38:51

an action to account SL log out and remember then  the API end point that we created the minimal API  

play38:56

I account SL logout the form what it is push so  when it push the form it's going to return home  

play39:05

page isn't it going to return to the homepage  yes so that's the log out and now once it get  

play39:10

the contest or the contest you can now sign out  peacefully okay if I am not um authorized register  

play39:21

and login we have in there if I'm authorized then  log out so with this we have to provide or maybe  

play39:30

you can say this is register this is log out this  is login and that is all so let's have a register  

play39:36

and now login so this is a register we must  provide the route this login from the register on

play39:43

top so this is register okay so this is ready  now now if I click on any page so for now don't  

play39:57

worry we're going to run this application and  test it out first but before we test this app  

play40:03

we need to create a class that's going to  H from the allate provider and that's going  

play40:10

to implement um the component State or the  authentication state from the server okay  

play40:16

in the client so let's authentication we're  going to add a class in here and now this  

play40:24

class going to be persistent Authentication  State provider this has inher from the main  

play40:31

class that is out State provider we need to  implement an abstract and that is the get  

play40:37

authentication State So within this we can have  a default orate task so let's assign it on top

play40:47

here then we going to check when a page is  being called being rendered this method is  

play40:58

going to get fired that is the the instance  of this so we need to create a property a  

play41:03

Constructor instance let's create Constructor for  this one so generate Constructor okay so this is  

play41:10

our Constructor and now within the Constructor  we going to specify this persistent component  

play41:16

State this is coming from the server Street and  I contain the state of the user so in here we can  

play41:22

now check if the person is authenticated then  we want to grab we want to create an instance  

play41:28

of this user info because this is what we  are passing in so we have to get this as  

play41:32

an out variable okay and now if it is n then  want to return return so we can just exclude  

play41:38

this and then make it just one line yes so if  it is null then so if it is not now then we're  

play41:44

going to have this variable being um holding the  values from the state then we can now create the  

play41:53

claims in here so we need to extract the claims  so we have the username ID we have um the name  

play42:01

and on the email so after doing that then we  need to call this aate and now we assign the  

play42:08

value to this this let me close this okay so  we the a the authentication STX that we created  

play42:18

earlier on here you can see so um we are going  to assign it to this because here we have a new  

play42:25

clim identity and that's a claim that is coming  in okay so we assign it to this after assigning  

play42:31

what are we doing then lastly this method to  get called which going to return authentication

play42:37

State you see so we can now go ahead we  can remove this con um the implementation  

play42:47

the class and now we are good to go so this  is all that we need to do we get the claims  

play42:52

from here we assign them and then we pass  on to the oate then we can now now tell the  

play42:57

state that yes we have what we need so please  keep on okay very simple one so after doing  

play43:06

this you go to solution you go to program  and now you need to register this in the

play43:11

program so here we're going to register  authorization call cascading upate then  

play43:20

we're going to add this as a single thing so  authentication State provider then that's the  

play43:24

instance that we created now in here okay so  that's all we need to do in here this now if  

play43:32

you are not authorized and want to access and  authorize uh content we want to redirect you  

play43:39

okay to log in so when you check this from  our route in here you can see that we added  

play43:47

this and this is the time for us to create a  component we have to create that component in  

play43:53

here so in the pages let's add the new razor  component Weir to login and now inside this  

play44:02

we need to inject our appdb content need inject  a nav manager and now we want to navigate you  

play44:08

this only the login not the account so login  slash and it's going to be the return URL so  

play44:15

we are passing into this right to the URL um  to this lugin so we can actually remove all  

play44:23

this maybe in case we don't need this we can  move it because I know using return URL for  

play44:29

now so all that you do here is navigate  to login and now Force load very simple  

play44:35

one you see that good now let's save this  so let's build the whole project again bu

play44:44

solution so we have an issue here that we need  to be fixed but could see it suceeded when you  

play44:53

click on this we need to include so to period  let's add the reference okay let's test this

play45:02

out yes we have an issue here because we have  not added the token authentication Service that  

play45:14

is the let's see okay so that's identity  so let's add it here so we services. add  

play45:24

authorization add authentication and we have  to specify the default scheme and the default  

play45:30

signing scheme and now do add identic cookies  you see that is this one now also let's check  

play45:39

from the register we did not create the UI you  see this so we have to create the the UI and in  

play45:49

that it is just an edit form that we need to  create which it's going to um have the form  

play46:00

for name email password and confirm password  so here I'm going to past this this an edit  

play46:09

form and now it has this the input for email  it has password it has confirm password okay  

play46:17

in that you know already that we have to create a  string property to handle the an error in here to  

play46:22

display So within this you can have this string  error in case we do have error but for now we  

play46:30

are not have we are not hand any error okay so  we can just remove this up let's take out this  

play46:37

error if you want to manage your any the errors  you can actually do that but we are not um doing  

play46:44

it now okay so this all we have a very simple  one with a button as register and now we're  

play46:49

going to submit to this register user method now  let's think let's run it again and then check it

play46:55

out okay so the app is ready and now let's see  we have a login and a register button in here  

play47:03

first of all you're going to register because  you haven't register so let's c um next CoD up  

play47:09

that's the name as you know and here I'm going to  use admin at admin password let me use admin at 1  

play47:16

2 3 admin at 1 23 click on register now let's  see wow nothing happens but let's check it out

play47:28

so from here I've opened the table if I refresh

play47:32

this oh okay you see now we have it in here you  see that is the name net code username and now  

play47:41

we have the noral usern name and the email okay so  usern name and our name we have it now let's go to

play47:51

login so if I click on login we have the email  and password click on login and I can see we  

play48:00

have the email in here so since the names have  appeared twice that is why it is taking the  

play48:09

first one and the first one I'm automatically  it is an email address if you want to grab the  

play48:15

second one and that is the actual your name  that we set then we have to look through the  

play48:20

claims and now we find out the names whereby  going to have the results as two then we get  

play48:25

the last one let's see how to work on that to  display not the email rather the name of the  

play48:31

authenticated person so go to our source code  and now from here that's our na menu that's  

play48:37

where we are having this so we can rather use  the next line so contest. user. claim. where  

play48:45

type is equal to our claim types. name and  now when you make it first you're going to  

play48:50

have the email so that last going to give us  the full name okay so let's re build this and  

play48:58

run it again and now let's check it out so you  might see the email you might see the name as

play49:02

well so let's log in again but before we log  in you going click on this inspect element then  

play49:11

we go to the application so you can see from  here the cookie we have no authentication in  

play49:17

here now click on login and now let's log in  and let's watch it out and see what going to  

play49:21

happen in here yes so see we have an token  that's a cookie token being up in here and  

play49:31

now you can see we have um admin at Gmail and no  admin at admin and then we have net code Hub that  

play49:37

is the name isn't it if I click on log out what  happens we see it is off and now this one is also

play49:42

off let's create another account so this turn R is  

play49:48

going to be Fred then there going to  be Fred at Fred password Fred at one

play49:57

23 let's create the

play50:01

account

play50:05

okay now let's log

play50:12

in okay so we are in now you see we have  net code H Fred at Fred and it's supposed  

play50:27

to be Fredick but you're still having  net code H you see this maybe let's

play50:33

see why are you having the same  net code when you go to the table  

play50:42

and now if you refresh this let's see  what we have Okay so let's open this

play50:51

again oh okay so it was let's go to up alleng  we did we did not change it so let's this Fred  

play51:05

Rick okay so that is this one we can now save  this and when we refresh the page let's see  

play51:13

what we're going to have since we change the  database you must see I'm Fred in here okay  

play51:21

let's rebild this again and I'll test it out  so I'll save this we run this okay and we have

play51:31

Frederick

play51:35

okay we still have this we're going

play51:41

to around

play51:44

this okay so let's log

play51:49

in now let's wait and see you must see Fredick  yeah that's what we have it let's log out let's  

play52:02

the account and maybe here we're going to  say your name your name is your name so

play52:11

u. U at1 23 U at three now we're  going to create an account here  

play52:24

now you can see the name your name  that is the name okay let's log

play52:30

in let's see yes so you can see we have your  name yeah this working okay so that is it  

play52:45

for this video we've seen how to create and  consume this service authentication Service  

play52:51

using cie customize one in net a BL Las web  app with interactive um web assembly render  

play52:59

mode I've made a video on the server mode  and also how to use JWT token in the server  

play53:05

mode so if you want to check those out  check the video description and you're  

play53:08

going to find it in there all right thank  you so much for watching this and I believe  

play53:12

this is learned something and hopefully I'm  going to catch up again so then take care

Rate This

5.0 / 5 (0 votes)

Do you need a summary in English?