简体   繁体   中英

How to do Authorization and Authentication in ASP.NET?

I was wondering how do I make it so when a user goes to a page its only displayed if he is logged in and secure, but if he is not logged in he is redirected to the login page? Thanks guys.

You can deny all un-authenticated users access in Web.config. When used with Forms Authentication denied users will be redirected to the login page specified:

<authentication mode="Forms">
  <forms loginUrl="~/account/login" timeout="2880" />
</authentication>

<authorization>
  <deny users="?"/>
</authorization>

FormsAuthentication将为您提供重定向和后续的Authorization cookie,但您需要诸如ASP.NET Membership之类的成员存储来跟踪密码和可能的角色。

when user get loggedin store the user values in Session and on page load event of the page check the the session variable values.

So you can get the user id authenticate or not.

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