【问题标题】:In asp.net mvc what is the use of context.User.Identity.Name?在asp.net mvc中context.User.Identity.Name有什么用?
【发布时间】:2013-01-09 18:35:08
【问题描述】:

我目前正在学习asp.net mvc,刚开始,我决定从web表单转向mvc。

我正在从 codeplex the mvc 音乐商店学习本教程,有这行代码我不明白它的使用方式和使用原因。

这是代码行:

if(!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] = context.User.Identity.Name;

                }

我想知道 context.User.Identity.Name 做了什么,因为我尝试删除它包含的 if 块并且应用程序仍然可以工作。

这是该函数的完整代码:

 public string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                if(!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] = context.User.Identity.Name;

                }
                else
                {
                    // Generate a new random GUID using System.Guid class
                    Guid tempCartId = Guid.NewGuid();
                    // Send tempCartId back to client as a cookie
                    context.Session[CartSessionKey] = tempCartId.ToString();
                }
            }
            return context.Session[CartSessionKey].ToString();
        }

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3 visual-studio


    【解决方案1】:

    当您需要身份验证时,它的作用与在 Web 窗体中所做的完全相同。一旦用户成功通过身份验证,context.User.Identity.Name 包含登录人的用户名。

    在您的特定示例中,它只是检查用户是否经过身份验证-尽管您应该检查 Request.IsAuthenticated 而不是检查用户名是否不为空或空白-并将此人的用户名放入 Session[CartSessionKey]

    【讨论】:

    • 感谢您的回答
    【解决方案2】:

    我建议你看看下面的链接: http://support.microsoft.com/kb/301240

    在应用程序中实施安全性并学习成员资格提供程序,它将让您了解对保持应用程序安全非常有用的角色和其他内容。

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多