Asp.Net Web 窗体访问母版页上的用户自定义属性
Asp.Net Web Forms access user custom properties on master page
如果 Web 窗体项目使用 Asp.Net 身份系统,我如何访问母版页上的自定义用户属性(例如名字、姓氏)?我已经做了这个配置 http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html 但我的项目不是 MVC 是 Web Forms。
默认情况下,我只能使用以下方式访问用户名:
Context.User.Identity.GetUserName()
没关系,我已经找到答案了。
在 Site.Master.cs 添加这行:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using EvSiProject.Models;
using Microsoft.AspNet.Identity.EntityFramework;
然后在 Page_Load() 函数中添加以下行:
if (Context.User.Identity.IsAuthenticated)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(Context.User.Identity.GetUserId());
//string fName = currentUser.FirstName;
//string lName = currentUser.LastName;
}
就是这样。
只是为了将我的两分钱加到 Ceparu 的回答中:
在Site.Master.cs中我还添加了一个public字段
public ApplicationUser CurrentUser;
在 Page_Load() 函数中更改了第二行以使用 public 字段
CurrentUser = manager.FindById(Context.User.Identity.GetUserId());
更改Site.Master.cs后,在Site.Master中可以使用
<%: CurrentUser.FirstName %>
而不是
<%: Context.User.Identity.GetUserName() %>
完成!
如果 Web 窗体项目使用 Asp.Net 身份系统,我如何访问母版页上的自定义用户属性(例如名字、姓氏)?我已经做了这个配置 http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html 但我的项目不是 MVC 是 Web Forms。
默认情况下,我只能使用以下方式访问用户名:
Context.User.Identity.GetUserName()
没关系,我已经找到答案了。
在 Site.Master.cs 添加这行:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using EvSiProject.Models;
using Microsoft.AspNet.Identity.EntityFramework;
然后在 Page_Load() 函数中添加以下行:
if (Context.User.Identity.IsAuthenticated)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(Context.User.Identity.GetUserId());
//string fName = currentUser.FirstName;
//string lName = currentUser.LastName;
}
就是这样。
只是为了将我的两分钱加到 Ceparu 的回答中:
在Site.Master.cs中我还添加了一个public字段
public ApplicationUser CurrentUser;
在 Page_Load() 函数中更改了第二行以使用 public 字段
CurrentUser = manager.FindById(Context.User.Identity.GetUserId());
更改Site.Master.cs后,在Site.Master中可以使用
<%: CurrentUser.FirstName %>
而不是
<%: Context.User.Identity.GetUserName() %>
完成!