Authentication vs Authorization

Authentication asks who you are; authorization asks what you're allowed to do. A web app proves who you are once, at sign-in, then remembers you with a cookie on every request after.

Apprentice · ASP.NET Core · Web Development · Updated

By

After this lesson you can

  • Tell authentication from authorization, and explain how a sign-in cookie keeps you signed in

Key takeaways

  • Authentication asks who you are, once at sign-in; authorization asks whether you're allowed to do this, on every protected page or action.
  • After sign-in, the server sends a signed, encrypted cookie, and the browser sends it back with every request, so the site knows it's still you.
  • A site never stores passwords as typed: it keeps a one-way hash, and compares hashes at sign-in.

Two security words sound alike and mean different things. Every app that has accounts needs both.

Who are you? Authentication

Authentication is proving who you are. At a website, that's usually a user name and a password: if they match, the site knows it's you.

What may you do? Authorization

Authorization is deciding what you're allowed to do, once the site knows who you are. Anyone may read the product list; only signed-in staff may open the order history; only an administrator may add products.

Authentication Authorization
Question Who are you? Are you allowed to do this?
When Once, at sign-in On every page or action that's protected
Fails with "That user name and password don't match." "You're signed in, but your account can't open that page."
In ASP.NET Core SignInAsync, after checking the password [Authorize], [Authorize(Roles = "Admin")]

An office building works the same way: your ID badge says who you are (authentication), and the doors it opens depend on your job (authorization).

Remembering who you are: the cookie

Each request to a web app is separate: the server doesn't remember the last one. So how does a site know it's still you on the next page?

  1. You sign in. The server checks your password.
  2. The server sends back a cookie: a small, signed and encrypted piece of data that says who you are and what roles you have.
  3. The browser sends that cookie back with every request to the site.
  4. On each request, the server reads the cookie, checks it hasn't been tampered with, and knows who you are, without asking for the password again.
  5. Signing out deletes the cookie.

That's cookie authentication, the usual way for sites made of pages, such as an MVC app. (Web APIs called from Angular usually send a token instead, which you'll meet in the Angular module.)

Passwords are never stored

A site must never keep passwords as typed. It keeps a hash: the result of a one-way scramble that can't be turned back into the password. At sign-in, it hashes what you typed and compares the two hashes. If the database is ever stolen, the passwords aren't in it.

Do it: tell a sign-in problem from a permission problem

NorthwindWeb has three pages, and each lets different people in. When someone can't open a page, the message has to say why: they haven't proved who they are yet (authentication), or they have and aren't allowed (authorization).

The method gets three values:

  • page: the page asked for, one of "Products", "Orders" or "AddProduct".
  • signedIn: true when the visitor has signed in, a bool.
  • isAdmin: true when the signed-in user is an administrator, a bool. It's always false when signedIn is false.

Who may open each page:

page Who may open it
"Products" Anyone, signed in or not
"Orders" Anyone who is signed in
"AddProduct" Only a signed-in administrator

The method returns one of three answers:

Answer When
"Allowed" The visitor may open the page
"Sign in first" The page needs a signed-in user, and nobody is signed in: an authentication problem
"Not allowed" The visitor is signed in, but isn't an administrator, and the page needs one: an authorization problem

For example, grace is signed in as staff, not an administrator, and asks for "AddProduct":

Question Here
Does the page need a signed-in user? Yes
Is someone signed in? Yes, grace
Does the page need an administrator? Yes
Is grace one? No

So Access("AddProduct", true, false) returns "Not allowed".

Your job: write the method Access(string page, bool signedIn, bool isAdmin) so it returns "Allowed", "Sign in first" or "Not allowed".

  • "Products" is "Allowed" for everyone.
  • "Orders" and "AddProduct" are "Sign in first" when signedIn is false.
  • "AddProduct" is "Not allowed" for a signed-in user who isn't an administrator.
  • Every other case is "Allowed".

Practiced live on the lesson page: write the C# right there and check it.

Check yourself

1 question to try on the lesson page.

So what

Authentication proves who someone is; authorization decides what they may do. A signed cookie carries the first from request to request, so the second can be checked on every page.

Key terms

Cheat sheets