HttpContext.Current.User.Identity.Name如何知道存在哪些用户名? [英] How does HttpContext.Current.User.Identity.Name know which usernames exist?

查看:363
本文介绍了HttpContext.Current.User.Identity.Name如何知道存在哪些用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不一定是问题,我只是好奇它是如何工作的.我有一种方法:

This is not necessarily an issue, I am just curious as to how it works. I have a method:

public static bool UserIsAuthenticated()
{
    bool isAuthed = false;
    try
    {
        if (HttpContext.Current.User.Identity.Name != null)
        {
            if (HttpContext.Current.User.Identity.Name.Length != 0)
            {
                FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;
                isAuthed = true;
                string MyUserData = ticket.UserData;
            }
        }
    }
    catch { } // not authed
    return isAuthed;
}

如果用户不存在,HttpContext.Current.User.Identity.Name返回null,但是它如何知道存在或不存在哪些用户名?

The HttpContext.Current.User.Identity.Name returns null if the user does not exist, but how does it know which usernames exist or do not exist?

推荐答案

HttpContext.Current.User.Identity.Name返回null

The HttpContext.Current.User.Identity.Name returns null

这取决于您的web.config文件中的身份验证模式是设置为表单还是 Windows .

This depends on whether the authentication mode is set to Forms or Windows in your web.config file.

例如,如果我这样编写身份验证:

For example, if I write the authentication like this:

<authentication mode="Forms"/>

然后,因为身份验证模式="Forms",我将为用户名获取null.但是,如果我将身份验证模式更改为Windows,如下所示:

Then because the authentication mode="Forms", I will get null for the username. But if I change the authentication mode to Windows like this:

<authentication mode="Windows"/>

我可以再次运行该应用程序并检查用户名,然后我将成功获取用户名.

I can run the application again and check for the username, and I will get the username successfully.

有关更多信息,请参见系统. Web.HttpContext.Current.User.Identity.Name与ASP.NET中的System.Environment.UserName .

For more information, see System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET.

这篇关于HttpContext.Current.User.Identity.Name如何知道存在哪些用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆