简体   繁体   中英

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);
        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM