且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asp.Net Web窗体母版页上的访问用户自定义属性

更新时间:2023-12-06 12:15:46

没关系,我已经找到了答案。

在Site.Master.cs添加此行:

 使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.Owin;
使用Owin;
使用EvSiProject.Models;
使用Microsoft.AspNet.Identity.EntityFramework;

然后添加此行对在Page_Load()函数:

 如果(Context.User.Identity.IsAuthenticated)
    {
        VAR经理=新的UserManager< ApplicationUser>(新UserStore< ApplicationUser>(新ApplicationDbContext()));
        变种的cu​​rrentUser = manager.FindById(Context.User.Identity.GetUserId());        //字符串FNAME = currentUser.FirstName;
        //字符串LNAME = currentUser.LastName;
    }

这就是它。

How can I access custom user properties(eg. first name, last name) on the master page if the Web Forms Project is using Asp.Net Identity System? I've already made this configurations http://www.itorian.com/2013/11/customize-users-profile-in-aspnet.html but my project is not MVC is Web Forms.

By default I can only access user's name using:

Context.User.Identity.GetUserName()

Never mind, I've found the answer.

On Site.Master.cs add this lines:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using EvSiProject.Models;
using Microsoft.AspNet.Identity.EntityFramework;

Then add this lines on the Page_Load() function:

    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;
    }

And that's it.