Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用母版页中的用户配置文件属性_C#_Asp.net_Webforms_Asp.net Identity - Fatal编程技术网

C# 使用母版页中的用户配置文件属性

C# 使用母版页中的用户配置文件属性,c#,asp.net,webforms,asp.net-identity,C#,Asp.net,Webforms,Asp.net Identity,如果用户登录,我想在所有页面上显示他们的名字 我正在使用VS2013.3和WebForms 到目前为止,我已经在用户注册时向用户配置文件添加了其他属性,包括名字 我想使用站点母版页(site.master)中的新配置文件信息。样板模板使用 <%:Context.User.Identity.GetUserName()%> 我对C#和ASP.net相当陌生 如何在site.master.aspx页面中引用新的用户配置文件属性?我需要在加载事件或类似事件中执行吗?或者有更好的方法吗?嘿,

如果用户登录,我想在所有页面上显示他们的名字

我正在使用VS2013.3和WebForms

到目前为止,我已经在用户注册时向用户配置文件添加了其他属性,包括名字

我想使用站点母版页(site.master)中的新配置文件信息。样板模板使用

<%:Context.User.Identity.GetUserName()%>
我对C#和ASP.net相当陌生


如何在site.master.aspx页面中引用新的用户配置文件属性?我需要在加载事件或类似事件中执行吗?或者有更好的方法吗?

嘿,我不知道你是否有解决方案,但我会发布我从你的问题和另一个问题中得到的想法,因为我也有同样的问题。我在identitymodel中将firstname和lastname添加到ApplicationUser,然后在site.master.cs中使用此代码

公共静态字符串GetFirstName()

{
字符串名;
ApplicationDbContext db=新的ApplicationDbContext();
UserStore UserStore=新的UserStore(db);
UserManager UserManager=新的UserManager(userStore);
ApplicationUser user=userManager.FindByName(HttpContext.Current.user.Identity.Name);
firstname=user.firstname;
返回名字;
}
我在母版页上用这个
希望能有帮助

var user = UserManager.FindById(User.Identity.GetUserId());
user.FirstName;
    {
        string firstname;

        ApplicationDbContext db = new ApplicationDbContext();
        UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
        UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

        ApplicationUser user = userManager.FindByName(HttpContext.Current.User.Identity.Name);

        firstname = user.FirstName;                              


        return firstname;
    }