显示原始 Sitecore 用户名而不是当前输入的用户名

发布于 2024-10-13 15:03:38 字数 759 浏览 2 评论 0原文

我可能会忽略这里明显的一些东西,但是是否可以返回 Sitecore 用户,其用户名采用注册时使用的大写形式?

目前,用户名将显示为用户登录时输入的任何内容,但我希望能够获取原始字符串。

我正在使用 User user = User.FromName(domainUser, false);

更新: 这就是在 Yan 出色的回答后我得到的结果:

// get the MembershipUser object normally by name
var initialUser = Membership.GetUser(domainUser, false);
if (initialUser != null)
{
    // get the same MembershipUser by Id - in this case it retuns username in correct case
    initialUser = Membership.GetUser(initialUser.ProviderUserKey, false);
}

// get the Sitecore user from the username in correct case
Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(initialUser.UserName, false);

谢谢,

Annelie

I might be overlooking something obvious here, but is it possible to return a Sitecore user with the username in the capitalisation they used when registering?

At the moment the username will be displayed as whatever the user typed when they logged in, but I'd like to be able to get the original string.

I'm using User user = User.FromName(domainUser, false);

UPDATE:
This is what I ended up with after Yan's excellent answer:

// get the MembershipUser object normally by name
var initialUser = Membership.GetUser(domainUser, false);
if (initialUser != null)
{
    // get the same MembershipUser by Id - in this case it retuns username in correct case
    initialUser = Membership.GetUser(initialUser.ProviderUserKey, false);
}

// get the Sitecore user from the username in correct case
Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(initialUser.UserName, false);

Thanks,

Annelie

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

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

发布评论

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

评论(1

笔落惊风雨 2024-10-20 15:03:38

这是一个有趣的问题,深入研究这个问题很有趣。

您提到的代码行实际上并没有去安全数据库来获取用户。它仅使用您提供的名称创建一个 User 对象。

您可以参考较低级别的 ASP.NET 类 System.Web.Security.Membership。它包含方法 GetUser,该方法已重载多次。诀窍在于,它接受用户名作为参数的重载版本的行为与User.FromName相同。我的意思是它不会从底层 SQL 表返回实际的 UserName ,而是使用您在参数中传递给 GetUser 方法的用户名。它仍然可以正常工作 - 所有比较均以小写形式完成。

因此,如果您确实需要获取用户名的真实大小写,则可以使用实际查询 UserName 字段的基础 SQL 源的方法之一。例如,GetUser(object, bool),它通过 UserId 获取用户:

  // get the MembershipUser object normally by name
  var user =  Membership.GetUser(@"sitecore\admin", false);
  if (user != null)
  {
    // get the same MembershipUser by Id - in this case it retuns username in correct case
    user = Membership.GetUser(user.ProviderUserKey, false);
    // use the user.UserName value as you wish
    Response.Write(user.UserName);
  }

如您所见,Membership.GetUser(username)< 是对数据库的 2 次调用,而不是 1 次。 /code> 而不是 User.FromName(username) 的 0。

或者,如果您确定用户有电子邮件,并且知道该电子邮件,则可以使用 Membership.GetUserNameByEmail(email) 方法。

That's an interesting question, and it was fun to dig into this deeper.

The line of code you mentioned doesn't actually go to the security database to get the user. It only creates a User object with the name you provided.

You can refer to the lower-level ASP.NET class System.Web.Security.Membership. It contains the method GetUser, which is overloaded a number of times. The trick is that its overloaded version which accepts username as a parameter behaves the same as User.FromName. I mean it doesn't return actual UserName from the underlying SQL table, but uses the username you passed in parameters to GetUser method. It still works correctly - all comparisons are done in lowercase.

So, if you DO need to get real case of the user name, you can use one of the methods which actually query the underlying SQL source for the UserName field. For instance, GetUser(object, bool), which gets the user by UserId:

  // get the MembershipUser object normally by name
  var user =  Membership.GetUser(@"sitecore\admin", false);
  if (user != null)
  {
    // get the same MembershipUser by Id - in this case it retuns username in correct case
    user = Membership.GetUser(user.ProviderUserKey, false);
    // use the user.UserName value as you wish
    Response.Write(user.UserName);
  }

As you can see, it is 2 calls to the database instead of 1 for Membership.GetUser(username) and instead of 0 for User.FromName(username).

Alternatively, if you know for sure the user has email, and you know this email, you can use Membership.GetUserNameByEmail(email) method.

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