http上下文。 current.user.identity.name 为空

发布于 01-08 02:15 字数 205 浏览 2 评论 0原文

我正在尝试将 httpcontext.current.user.identity.name 的值与存储在我的 sql 数据库表中的值进行比较。它在我的本地机器上运行良好。但是,当服务器上托管同一网站时,httpcontext.current.user.identity.name 返回空。在 IIS 中,“允许匿名访问”已启用,但我不想禁用它。网上有很多解决方案,但我找不到适合我要求的解决方案。

I am trying to compare values of httpcontext.current.user.identity.name with the values stored in my sql database table. It works fine in my local machine. But httpcontext.current.user.identity.name is returning empty when the same website in hosted on server. In IIS "Allow anonymous access" is enabled and i don't want to disable it. There are many solutions available online but i am not able to find the once that's suits my requirement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何必那么矫情2025-01-15 02:15:05

您是否尝试过设置 AuthCookie?

if (Membership.ValidateUser(txtBxusername, txtBxpassword))
{
    FormsAuthentication.SetAuthCookie(strUsername, true);
    bool isAuth = User.Identity.IsAuthenticated; 
    FormsAuthentication.RedirectFromLoginPage(userName, chkBxRememberMe.Checked);
}

现在您应该能够在代码中的其他位置使用以下内容:

string userName = User.Identity.Name; 

Have you tried setting the AuthCookie?

if (Membership.ValidateUser(txtBxusername, txtBxpassword))
{
    FormsAuthentication.SetAuthCookie(strUsername, true);
    bool isAuth = User.Identity.IsAuthenticated; 
    FormsAuthentication.RedirectFromLoginPage(userName, chkBxRememberMe.Checked);
}

Now you should be able to use the following elsewhere in your code:

string userName = User.Identity.Name; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文