user55665484375
user55665484375

Reputation: 181

How do you require users to be logged in on asp.net

I am new to asp.net Identity and I cannot seem to figure out how to only allow logged in users to access certain pages in my controllers. All the documentation I am reading seems dated. Basically I would like to forward the user to a login page when they make a request to the server which runs one of the specified controller functions like so

       //How do I prevent anonymous user from preforming this model action
       //Unless they are logged in

       [HttpPost]
       [ValidateAntiForgeryToken]
        public async Task<IActionResult> SomeModelAction([Bind("ID,AModelField,UserID")] AModel aModel)
        {
         //code to preform action on a model

            return View(aModel);
        }

Upvotes: 1

Views: 838

Answers (1)

awh112
awh112

Reputation: 1484

You can add an [Authorize] attribute to a class or method you'd like to be available only to authenticated users.

Take a look here for some up-to-date documentation and a few examples.

Upvotes: 3

Related Questions