Basic Authentication - Create Attribute

9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€42.95
List Price:  €60.14
You save:  €17.18
£37.30
List Price:  £52.23
You save:  £14.92
CA$68.86
List Price:  CA$96.41
You save:  CA$27.55
A$76.77
List Price:  A$107.49
You save:  A$30.71
S$64.40
List Price:  S$90.17
You save:  S$25.76
HK$390.19
List Price:  HK$546.29
You save:  HK$156.10
CHF 40.24
List Price:  CHF 56.34
You save:  CHF 16.10
NOK kr500.18
List Price:  NOK kr700.29
You save:  NOK kr200.11
DKK kr320.58
List Price:  DKK kr448.84
You save:  DKK kr128.25
NZ$85.35
List Price:  NZ$119.50
You save:  NZ$34.14
د.إ183.58
List Price:  د.إ257.03
You save:  د.إ73.45
৳6,086.89
List Price:  ৳8,522.14
You save:  ৳2,435.24
₹4,406.11
List Price:  ₹6,168.91
You save:  ₹1,762.79
RM211.60
List Price:  RM296.26
You save:  RM84.66
₦76,597.67
List Price:  ₦107,242.87
You save:  ₦30,645.20
₨14,190.18
List Price:  ₨19,867.39
You save:  ₨5,677.20
฿1,619.37
List Price:  ฿2,267.25
You save:  ฿647.88
₺2,057.23
List Price:  ₺2,880.29
You save:  ₺823.05
B$272.20
List Price:  B$381.10
You save:  B$108.90
R885.95
List Price:  R1,240.41
You save:  R354.45
Лв83.75
List Price:  Лв117.26
You save:  Лв33.50
₩69,716.63
List Price:  ₩97,608.87
You save:  ₩27,892.23
₪169.52
List Price:  ₪237.34
You save:  ₪67.82
₱2,873.05
List Price:  ₱4,022.50
You save:  ₱1,149.45
¥7,426.70
List Price:  ¥10,397.98
You save:  ¥2,971.27
MX$937.87
List Price:  MX$1,313.09
You save:  MX$375.22
QR182.54
List Price:  QR255.58
You save:  QR73.03
P718.20
List Price:  P1,005.53
You save:  P287.33
KSh6,458.70
List Price:  KSh9,042.70
You save:  KSh2,584
E£2,424.49
List Price:  E£3,394.48
You save:  E£969.99
ብር7,102.60
List Price:  ብር9,944.21
You save:  ብር2,841.60
Kz45,588.63
List Price:  Kz63,827.73
You save:  Kz18,239.10
CLP$48,399.31
List Price:  CLP$67,762.91
You save:  CLP$19,363.60
CN¥356.46
List Price:  CN¥499.08
You save:  CN¥142.61
RD$3,145.73
List Price:  RD$4,404.27
You save:  RD$1,258.54
DA6,491.70
List Price:  DA9,088.91
You save:  DA2,597.20
FJ$113.07
List Price:  FJ$158.31
You save:  FJ$45.24
Q383.67
List Price:  Q537.17
You save:  Q153.50
GY$10,468.08
List Price:  GY$14,656.15
You save:  GY$4,188.07
ISK kr6,167.76
List Price:  ISK kr8,635.36
You save:  ISK kr2,467.60
DH450.01
List Price:  DH630.06
You save:  DH180.04
L830.88
List Price:  L1,163.30
You save:  L332.42
ден2,628.38
List Price:  ден3,679.94
You save:  ден1,051.56
MOP$402.01
List Price:  MOP$562.85
You save:  MOP$160.83
N$879.83
List Price:  N$1,231.84
You save:  N$352
C$1,842.15
List Price:  C$2,579.16
You save:  C$737
रु7,061.21
List Price:  रु9,886.26
You save:  रु2,825.05
S/176.79
List Price:  S/247.52
You save:  S/70.73
K211.93
List Price:  K296.72
You save:  K84.79
SAR187.57
List Price:  SAR262.62
You save:  SAR75.04
ZK1,182.65
List Price:  ZK1,655.81
You save:  ZK473.15
L218.18
List Price:  L305.47
You save:  L87.29
Kč1,051.46
List Price:  Kč1,472.13
You save:  Kč420.66
Ft17,017.21
List Price:  Ft23,825.45
You save:  Ft6,808.24
SEK kr472.64
List Price:  SEK kr661.74
You save:  SEK kr189.09
ARS$68,822.84
List Price:  ARS$96,357.49
You save:  ARS$27,534.64
Bs345.87
List Price:  Bs484.25
You save:  Bs138.37
COP$202,235.45
List Price:  COP$283,145.81
You save:  COP$80,910.36
₡25,306.81
List Price:  ₡35,431.55
You save:  ₡10,124.74
L1,310.57
List Price:  L1,834.90
You save:  L524.33
₲361,558.37
List Price:  ₲506,210.65
You save:  ₲144,652.28
$U2,000.31
List Price:  $U2,800.60
You save:  $U800.28
zł183.28
List Price:  zł256.61
You save:  zł73.32
Already have an account? Log In

Transcript

Hello guys, welcome to the 24th session or the SP dotnet way VBA to in a previous session we created a user security class in which we created a one static login method. In this session we are going to create an attribute for the or basic authentication. Let's switch to Visual Studio. Right click and select Add a new class with your name vz ot attribute and click Add. We are going to action this class from the authorization filter attribute or toe arises and filter attribute which was present in a different namespace. So we need to include and we are going to override on auto method or third class.

Please do enter. We know that the each request contain the header. We already use the excerpt from which we can specify the return data for the or request. For the authentication, we need to use the authorize header. Let's first of all check whether the header is English or not India or excellent contain which is the parameter for that function. Insert context.request.headers.org authorizes and equal equal No.

If the header is not present, then we need to response the unauthorized status score. For that we need to write the excellent context dot response equal to x and context dot request. Create response which was present in another namespace. So, we need to import within a record we need to specify the status code that is on authorize other Otherwise Otherwise we need to obtain the token from the power hitter frame auth token equal to excellent context dot request dot headers door authorization door to parameter and we need to decode that token string decoded token equal to encoding for that arena to use the using system dot txt statement dot UTF eight dot get string and we need to pass the lower token in the form of the base 64. Therefore, we need to write the Convert Lord from May 64th string and pass do auth token and semicolon from which we can easily identify the username and the password pair.

However, it was in a single string therefore, we need to split into the two part. For that we need to write to string good reading co equal to decoded token dot split from semi colon after the split Including zero error content the two things the first will be username and the second will be a password. And now we need to check that the given username and password pair are match or not as per the database. For that we already created user security class, we just need to call its login method. So right if user security dot login and pass the two thing that are creating here, credential of zero, and credential of one and press enter. If the user match, then we need to generate the if the username and password match, then we need to store in the current principal of the tree.

For that we need to write the trade and import the it's an interface. Trade door to current principle equal to new genetic coding SEPA which was present in different ways for added a new genetic identity. For that, we need to pass the login username. So we are using the a credential of zero and five to rule that will be Anil, India lowercase and the semicolon. If the username and the password will not match, then the logged in user will be authorized. Therefore, we need to return the ITP status code for the unauthorized let's copy the admin that we are using previous if and paste it.

Now we need to use that basic author attribute on our controller, open the our controller and add the accent label, we are going to write the basic auth musical that attribute are created by the US. Let's run the our solution. Let's copy the URL and open in postman right here API slurs employee and click on DSN. You can see that it's written the a 401 unauthorized status code. This is why because we are not specify the authentication for the request from the authorizers and Deb select the basic auth from the drop down and write the username and password. I am already read the t statistic.

Now I am sending it You can see that we are successful add to the list of record However, those record are for the developers. Because we are traditionally used the because we as traditionally use the user name developer in the our case, that's why we are receiving the result for the Dow only. Let's modify. For this we need to write the trade which was a prison in different industries. So I added a lot current principal door identity dot name, which will return the logged in user name. Let's run the our solution.

Switch to postman. Click on the send button. You can see that it written the areas of all the T shirt on You can see there to return the result of the employed that are belong to the testing department. If we are sending data for the developer, then it will return the result for the variable only. If we are writing the wrong password, then again it generate the 401 unauthorized status. First of all, we need to create one class that action from the authorization filter attribute.

And we need to override the on authorization method of it. Within that we need to check whether the authorization header is present in the request or not. If it was not present, then user is unauthorized. If the present then we need to retrieve the token and decode it. And we need to check whether the given credential valid or not? If it was valid then we need to show in the current principle of the trade.

Otherwise we need to send the unauthorized status code. Thanks for watching. Have a nice day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.