Lecture thumbnail 0:00 / 0:00 let us try to implement registration
functionality in this application
so if you make a request to the route
API slash version number slash account
slash register it has to read the user
details from the request body and insert
the same into the asp.net users table by
using identity
and Returns the corresponding response
that includes with the user details so
that you can create the registration
page or registration component in the
angular application
exactly in this lecture let us try to
create endpoint for registration
first we have to create register dto to
transfer the data from the client
application to the controller
so right click on the core project
add new folder
folder name is dto
and right click on the dto folder add
new item
select code class file name is
register dto.cs
add button
make it as a public class you need not
inherit from any other predefined class
because it is just an independent class
so you can add additional properties
that you want
for example person name email whichever
the values that you would like to
receive from the client application that
is angular application
in addition to this you can add data
annotations for validations
for example the person name is mandatory
so we are going to add required
attribute import the namespace system
dot component model dot data annotations
required with error message
and then one more for email
pattern validation
and optionally remote validation in
order to report duplicate email address
so when the user submits email address
value it has to automatically make a
request to ease email already registered
it has to automatically post this email
address value to this action method that
is in the account controller
and that action method should return
either true or false
if it returns true that indicates the
value is valid that means the user is
not registered already so it can be
registered
in case if the value is invalid that
action method should return false
so the controller name and action method
name over here
and then error message
the goal here is we have to report if
that email address is already registered
by somebody else
optionally you can initialize the empty
value or string value in the person name
and all the related string properties
in order to use this remote validation
you’re required to install the package
called Microsoft dot
asp.co.nvc.view features
you can also install the same manually
right click on dependencies of the core
project manage nuget packages
and search for the package
Microsoft Dot
asp.net core Dot
MVC Dot View features
so install that package
select the same version as of me 2.2.0
install
you are able to access this remote
attribute
by importing the namespace microsoft.a
spirit core.mvc
okay as a continuation you can create
the property for phone number
assuming the phone number is also
mandatory
or playing regular expression for this
so we are accepting the digits only and
unlimited characters
indicates more than one character
and then for password
and then for confirmation password
it should match with the password value
okay
we have created essential properties in
the register dto
now let’s create an end point means an
action method in the account controller
so we have not yet created a conch
controller right click on the
controllers folder particularly V1
folder of controllers
add new controller
select API and select API controller
empty
specify the controller name account
controller
dot CS is the default file extension
here
and instead of you apply the route and
API controller attribute explicitly
inherit from our custom controller base
so in our custom controller Base Class
we have already applied API route as
well as API controller attribute
okay
and
allow these to be accessible anonymously
without user registration and login
means we are going to disable
authorization for this controller
so import the namespace asp.net core dot
authorization
and also
apply the API version that is 1.0 for
this case
inject user manager sign in manager and
role manager into this account
controller
import essential namespaces asp.net core
dot authorization asp.net core dot
identity Etc
and also citiesmanager dot core dot
identity
in the Constructor inject the same
you require user manager in order to
manage the users such as creating
deleting or updating the user and
searching for the users adding the user
for the particular user role Etc
and then you required sign in manager in
order to sign in or sign out to the user
and you require a role manager in order
to manipulate the roles such as creating
or deleting the roles
so after you instantiate and initialize
the same into the respective years
next after that create an action method
in the account controller
that is a post request that is a
registration
and at the end we are going to return
application user object as return value
we are going to receive register dto
and the method name is post register
import the namespace
citiesmanager dot core Dot dto
because you have to access this register
dto class
so assume that we have received the
registered dto
I mean assume that we have received all
these values that is person name email
Etc
from the client application
and then you have to insert the same
into the asp.net users table by using
user manager
explicitly apply the HTTP Post in order
to specify that we are going to receive
the post request through this action
method
and here add the code for validation
if the model state is invalid
then collect all the error messages
in the model State DOT values you have
all the error messages and one value can
have more than one error message
so the list of error messages is a
collection here so select many
collect all the errors and collect error
message from each error
so at the end you will be having a list
of error messages as string
and join all of them
by using the separator for example a
pipe symbol or a comma
and that joint string will be the error
message
that can be reported to the client by
using return problem
you have to do this explicitly because
the built-in code for returning the
problem response
doesn’t include all these errors from
the identity so you have to write it
explicitly
so if the model state is invalid means
at least one field or one property has
at least one error message
you are going to return the problem
response
otherwise we will continue since the if
condition is false over here that means
if the model state is valid we will
continue over here
we will create a new object of
application user class
and initialize all essential properties
importantly email address
the phone number
here the email address itself is the
username using which the user can log in
and the person name explicitly
okay these are essential properties that
are required
now a user manager
create this user
we have the user object right so create
this user
and we are going to explicitly pass the
password value
so it internally generates the password
hash automatically before storing the
same in the database
and since it is a synchronous method use
a weight keyword and it is going to
return identity result which includes
with the status of registration
if the registration means if insertion
is successful into the asp.net users
table
then the result dot succeeded will be
true
so we will check here if it is succeeded
means if the insertion into the asp.net
user stablely success
then we can sign in the user
hey sign in manager
sign in this particular user
and
is persistent equal to false means the
authentication cookie is not a
persistent cooking it must be deleted
automatically when the browser is closed
and since everything is happened
correctly means we have successfully
inserted the user details into the
asp.net users table and signed in the
same
after that return the OK response
along with the user details
but otherwise
if the insertion is failed means
succeeded equal to false
in this case collect the error messages
from the identity result
separator a is a pipe symbol
in the identity result we have errors
collection that contains elements
where each element contains a
description property
that contains actual error message
so the errors property is of I
enumerable of identity error type
the identity error contains actual error
message including error code and
description property
so we are reading all the descriptions
and joining them by using string.join
and this pipe symbol is a separator
between every error message
so Error 1
pipe symbol error 2 like this all the
error messages will be collected into
this variable and we will return the
same
problem result with error message
okay this thing happens only if the
insertion is failed due to some reason
for example a duplicate email address or
any other error
okay this is the code for user
registration
we have received the user details by
using register dto over here
and if model state is invalid we will
return their messages as problem result
creating the user object inserting the
same into the ASP net users table
if it is insertion is successful we will
sign in the user and return the same
user details as response
otherwise we will return the error
details that occur while inserting the
data
in addition to this since we have
enabled remote validation
we have to create this action method
that is easy email already registered
okay let it be ease email already
registered
and create the same as an action method
over here
so we will receive the email address as
argument
by the way it is get request
so it makes a get request to this
account slash is email already
registered and supplies the email
address value as a query string
parameter
so we will receive the same over here
and then we will check hey user manager
I would like to search the user based on
the email address
receiving the application user object
into this user variable
so if the email address already exists
in the database we will receive the user
object here otherwise we will return
null
then we will check we are expecting it
to be null
means for example we have entered ABC at
example.com
since that value does not exist already
the user object will be null
so it is nullable type
if it is now
we can return true
otherwise false
okay we will return a success that is
okay result because false here is not an
exception it is just a proper
information to the client that that user
is already registered
so you can use OK even for false case as
well
so when you make a post request to this
register
as a part of the model binding into this
register dto it automatically calls this
remote validation that is ease email
already registered
so that email address value will be
checked here if that email already
registered by somebody else by an
existing user then we will return false
if that data already not exists in the
asp.net users table
that indicates that the email is
available so we will return true in that
case
and also optionally in the program.cs
file
enable static files
so that the clients can access the files
that are present in the www root folder
not required currently but may be
required in feature
okay
so in the next lecture we are going to
create user interface for registration
in angular
Play Stop Play Play Play Play Play Play Play Play Play Start Start Start