如果Web窗体项目使用 Asp.Net Identity System ,如何在母版页上访问自定义用户属性(例如,名字,姓氏)?我已经完成了这个配置http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html,但我的项目不是MVC是Web Forms。
默认情况下,我只能使用以下方式访问用户名:
Context.User.Identity.GetUserName()
答案 0 :(得分:0)
没关系,我找到了答案。
在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;
}
就是这样。
答案 1 :(得分:0)
只需将我的两分钱加到Ceparu的回答中:
在Site.Master.cs中,我还添加了一个公共字段
public ApplicationUser CurrentUser;
在Page_Load()函数中更改了第二行以使用公共字段
CurrentUser = manager.FindById(Context.User.Identity.GetUserId());
更改Site.Master.cs后,在Site.Master中可以使用
<%: CurrentUser.FirstName %>
而不是
<%: Context.User.Identity.GetUserName() %>
完成!