【问题标题】:How do you get a User Identity?您如何获得用户身份?
【发布时间】:2012-09-06 19:37:48
【问题描述】:

一旦用户通过身份验证和授权,应用程序就可以通过使用 User 对象的 Identity 属性来获取有关用户的信息。 Identity 属性返回一个包含用户名和角色信息的对象。

下面是我用来理解这个概念的代码sn-p:-

private void Page_Load(object sender, System.EventArgs e)
{
 Label1.Text = User.Identity.IsAuthenticated.ToString();
 Label2.Text = User.Identity.Name;
 Label3.Text = User.Identity.AuthenticationType;
}

还有其他方法可以获取用户身份吗?

【问题讨论】:

  • 我不明白。为什么你需要另一种方式来获得它?
  • 我只是在搜索以找到其他出路。虽然这对我有用。我只是在询问我是否也可以找到其他方法......
  • 如果您只希望在调用 Page_Load 时从 ASP.NET WebPage 获取用户身份,请创建一个字符串 [] 并执行以下字符串 strRawUser = Page.User.Identity.Name;然后从那里 strRawUser[0] 将拥有域,而 strRawUser[1] 将拥有用户登录 ID

标签: c# asp.net


【解决方案1】:

如果您想要以下内容,请说明..

如果您只希望在调用 Page_Load 时从 ASP.NET WebPage 获取用户身份,请创建一个字符串 [] 并执行以下操作

string strRawUser = Page.User.Identity.Name;

然后从那里 strRawUser 将有类似 "DomainName\UserName" 所以你需要将字符串拆分成一个字符串数组并像这样获取字符串[1]值

string[] strRawUserSplitter = Page.User.Identity.Name.Split("\\");
Label2.Text = strRawUserSplitter[1]

【讨论】:

  • 很高兴我能为您提供一个简单的解决方案,快乐编码:)
  • 非常感谢 DJ KRAZE...祝你编码愉快 :)
【解决方案2】:

您还可以使用 Principal Object 获取当前用户身份。

【讨论】:

    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2018-02-17
    • 2020-05-03
    • 2021-11-03
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多