听起来表单身份验证可以处理您需要的内容。将以下行添加到您的根 web.config
<authentication mode="Forms">
<forms name="XXXXX.ASPXAUTH" timeout="60" loginUrl="~/login.aspx" protection="All" path="/"></forms>
</authentication>
将 XXX 替换为您想要调用 cookie 的任何内容。还将 login.aspx 重命名为您为登录页面命名的任何名称。此代码会将未通过身份验证的任何人重定向到登录页面。
然后,在您的登录逻辑中使用类似于以下 C# 代码的内容
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUsername.Text,
DateTime.Now, DateTime.Now.AddMinutes(60), true, reader["user_level"] + "",
FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
使用此代码,您将希望发送登录人的用户级别(即管理员、用户等),其中我有“读者 [...”
您需要做的最后一件事是使用自己的 web.config 设置每个受保护的目录,其中概述了允许的用户角色和拒绝的角色。您在 web.config 中用于角色的名称需要与发送到 FormsAuthenticationTicket 的值一致,这样您就可以开始使用了。