Authenticate, Authorization and Claim. All you need to know in ASP.NET Core
Tags: authentication, authorization, claim, identity
Published: 2017-11-05 17:33:57

---
title: Authenticate, Authorization and Claim. All you need to know in ASP.NET Core
published: true
description: Hello guys, in this article I'm going to explain about the concepts about authentication, authorization and claim. I will show too how we can using claim for authorization in ASP.NET Core.
tags: authentication, authorization, claim, Identity
---

When we are talking about authentication and authorization a mix of mess arrive in your head.
 
The first thing that we have to know is: 

<ul>
<li>Authentication is a process to know who you are.</li> 
<li>Authorization is the process to know if you can do what you want.</li>
</ul>

Is easier now? I will show an example. 

When you go to access a site and for this is needed inform a login and password, this mean that you need to be authenticated to know who you are. 

You access the site and when you try to access some part, as report or admin module, and the site alert you that lack privilege, this process is called authorization. That is, you have authentication but not authorization.  

Build an application that take care both authentication and authorization is very hard. But for those who work with ASP.NET Core is recommended to use something called Identity. 

Identity is a feature of ASP.NET Core that facilitate the process to create user account, authentication and user privileges (authorization).  

Using Identity with Entity Framework get more beautiful, because the Identity will use the Entity Framework's wisdom to access the data base. 

But, where the claim starts? 

Keep calm, I'm going to explain. 

In summary, the claim is an attribute. A person has many claims, below I explain better. 

All person has not an attribute but many. A person has a name, weight, height, birth date and many others. This is claim set.
 
Each claim has an issuer, that is, who issued the claim. When I talk that a person has a weight and height, himself is the issuer. But when I talk about social number of a person, the issuer is the federal government. 

Another example. When you make an integration with Facebook and get person data the issuer is Facebook. Do you understand now what is claim and issuer?  

What if I use Identity, how use claims? 

When the application is using Identity and authenticate a user, as code below, this authentication creates an object of type "ClaimsPrincipal".  

<img src="http://www.stephanybatista.com/wp-content/uploads/2017/10/claim-img1.png"/>

For those who do not know Identity, the "SigninManager" class is used to authenticate a user with a name and password. But note, I do not need to access the database to see if the user exists, the Identity with Entity Framework makes this for me. 

Ok, from now any controller has a property called "User" of type "ClaimsPrincipal". For example, when using in debug mode, I get all claims of a user authenticated. 

<img src="http://www.stephanybatista.com/wp-content/uploads/2017/10/claim-img2.png"/> 

If you desire, you can add others claims. Above I add a claim called "Employeer" through of object "UserManager". 

<img src="http://www.stephanybatista.com/wp-content/uploads/2017/10/claim-img3.png"/> 

Note that after create a user, through of method "CreateAsync", I am using the same object to add a claim for user.  

In this sample, I am giving this attribute for user that is inserted and thus the application can validate that only the users with this attribute can access a part of system. Do you remember when I talked about authorization? Let's do now.
  
In order to create this authorization, we must before create our policies in C#. In Startup class, method "ConfigureServices", I insert a policy. 

<img src="http://www.stephanybatista.com/wp-content/uploads/2017/10/claim-img4.png"/> 

Note that I created a policy called "OnlyEmployees" and to meet this the user must have this attribute. 

Created the policy, now the application can deal with authorization in the controllers. In the code below, I am using a controller called "ManageController", where only those who meet the policy can access, otherwise, the user will be redirect to other page with access denied. 

<img src="http://www.stephanybatista.com/wp-content/uploads/2017/10/claim-img5.png"/> 

Like you can see, the logic of attributes and issuers facilitate to manage users in our application. 

<b>Conclusion</b>

The Identity facilitates to develop an application that need of user account and authorization. But before, we must know basic concepts as authentication and authorization and claims. 

<b>Did you like this article?</b> Would you know more about .Net Core? I created a course at Udemy with a cheap price. Access https://www.udemy.com/aspnet-core-20-learn-concepts-and-creating-an-web-app