【问题标题】:Identity Framework cant find "User" on masterpage?身份框架在母版页上找不到“用户”?
【发布时间】:2014-09-08 08:53:36
【问题描述】:

所以我按照本教程在 .net 中安装和使用身份框架。 http://www.asp.net/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project

在 register.aspx 和 login.aspx 页面上一切正常。

我想从登录页面中获取这个逻辑...

if (User.Identity.IsAuthenticated) {
    //do something...
}

并将其放在母版页的 Page_Load 中(以便我可以显示登录或注销链接,具体取决于)

但是,当我这样做时,我收到一条错误消息

当前上下文中不存在名称“用户”

我在母版页后面的代码中有以下“使用”,这里的所有内容都在找到“用户”的 login.aspx 页面上。

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using SugarSpun_v2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

我有点困惑!您如何使用母版页上的身份框架?我实际上希望最终将登录表单集成到母版页本身,所以这可能会给我带来问题!

感谢任何帮助,只是刚刚发现了 Indentity Framework,所以请温柔:)

【问题讨论】:

    标签: c# .net webforms asp.net-identity


    【解决方案1】:

    试试看HttpContext.Current.User是否可以访问。

    【讨论】:

      【解决方案2】:

      如果您仔细查看您所指的User.Identity.IsAuthenticated,它不是 Asp.Net Identity 框架的一部分。 User 类作为 IPrincipal 提供,它是 .Net 框架的一部分。 User.Identity 正在实现IIdentity 接口,它也是.Net 框架的一部分。

      AspNet Identity 框架所做的是将ClaimsPrincipal 设置为HttpContext.Current.User,其中ClaimsIdentity 设置为IIdentity

      因此,您所有使用 HttpContext.Current.User 的代码在新框架下都应该可以正常工作(在所有项目转换中对我都有效)。

      @Kippie 正确回答后,HttpContext.Current.User 应该会为您提供登录用户的详细信息。只要有 HTTP 请求发生。

      【讨论】:

      • 当然,谢谢!我知道这会很简单,我只是想不到!两者都回答正确,但将其标记为更全面:) 谢谢你们!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多