1

我使用带有配置文件的 Windows 身份验证,并希望切换到自定义身份验证。
我的问题是如何指定我的用户已通过身份验证以及如何设置 Profile.UserName。
我知道 Profile.UserName 是 ReadOnly 。

在我的 Web.Config 中,我将 authentication mode="None"IIS 更改并配置为启用匿名。
在 global.asax 中,我验证用户是否存在 Cookie,如果不存在,则将用户重定向到登录页面。当他提交时,我创建了 cookie,此时,我将设置个人资料信息。

如果有人可以给我一些关于此的链接,我将不胜感激。

4

1 回答 1

1

听起来表单身份验证可以处理您需要的内容。将以下行添加到您的根 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 的值一致,这样您就可以开始使用了。

于 2010-09-29T16:02:10.263 回答