c# - 获取经过身份验证的用户的信息(不是计算机名称)

标签 c# asp.net authentication

我有一个登录页面,它调用以下事件处理程序:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{        
    if (Login1.UserName == "abc" && Login1.Password == "1234"){
        if (Request["ReturnUrl"] != null)
        {
            FormsAuthentication.SetAuthCookie(userName, false, "AdminPart/AdminHome.aspx");
            FormsAuthentication.RedirectFromLoginPage(userName, Login1.RememberMeSet);
        }
        else
        {              
            FormsAuthentication.SetAuthCookie(userName, false, "AdminPart/AdminHome.aspx");
            Response.Redirect("HomePage.aspx");
        }
    }    
}

我的 web.config 文件是这样的:

<system.web>
  <authorization>
    <deny users="?" />
  </authorization>

现在,我试图在我的 HomePage.aspx.cs 中获取经过身份验证的用户的用户名,但是 User.Identity.Name返回我的电脑的名称。我尝试将以下代码添加到配置文件中:

<authentication mode="Forms">
    <forms loginUrl="~/login.aspx" timeout="10" protection="All"></forms>
</authentication>

但是 <authentication mode="Forms"> 行再次出现问题.添加此行后,在我的主页中,IDE 对以 <% Page ... %> 开头的第一行发出警告会出现运行时错误。

我用了HttpContext.Current.User.Identity.Name ,也不行。 我只是想在项目的不同部分获取经过身份验证的用户的名称。因此,如果需要,我可以更改逻辑。 任何帮助将不胜感激。

编辑:我在 Windows 7(64 位)上使用 Visual Web Devepoler 2008 Express。

EDIT2:问题以某种方式解决。答案如下。如何?为什么?不能以编程方式完成吗?我不知道,我想知道为什么。

最佳答案

这是我找到的解决方案:首先,我删除了 web.config 文件中的授权标记。然后在解决方案资源管理器窗口中,单击 Properties 图标并打开 ASP.NET 的配置网页。然后我点击了 Security tab->Set up authentication。我选择了带有 Internet 连接的选项,现在我可以在任何地方通过 Page.User.Identity.Name 访问经过身份验证的用户名。

关于c# - 获取经过身份验证的用户的信息(不是计算机名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10003052/

相关文章:

c# - 如何使用 GridView 创建每行 4 列的动态表?

c# - 将 SqlDataSource 移动到类文件 ASP.NET

Django APIClient 登录不起作用

c# - 将字符串变量传递到网络凭据构造函数中作为密码参数

c# - 数据序列化 WPF

c# - 使用 GET 执行 WebRequest 时出现 ProtocolViolationException

c# - Agility Pack - 为多个 HTTP 请求维护 'connection'

c# - 如何从用户控件更改 MasterPage 中的控件值?

session - 如何增加 Plone 登录 session ?

c# - 无法读取 Excel 文件 - File.open 无效