ASP.NET身份扩展的方法来访问用户属性 [英] ASP.NET Identity Extend methods to access user properties

查看:122
本文介绍了ASP.NET身份扩展的方法来访问用户属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我可以扩展方法来访问用户属性?

有类似的方法:

  User.Identity.GetUserId()
User.Identity.GetUserName()

这是从的意见和控制器访问。

我要像方法来扩展此功能:

  User.Identity.GetUserPhoneNumber()
User.Identity.GetUserLanguaje()


解决方案

类似的问题:需要获得用户更多的用户属性.Indentity微软专业人士codePLEX工作日志的回答如下


  

你可以得到User对象做User.Email或User.PhoneNumber因为这些属性都挂在用户模式


我们可以得到在ASP.Net身份应用当前的用户对象,如下,从那里你可以访问用户对象的所有属性,我们按照相同的mvc5 Web的应用程序,截至目前。

  //帐号控制器这样的
变种的cu​​rrentUser = UserManager.FindById(User.Identity.GetUserId());

在其他的控制器则需要以下内容添加到您的控制器:

  VAR经理=新的UserManager< ApplicationUser>(新UserStore< ApplicationUser>(新ApplicationDbContext()));  //获取登录用户当前和查找在ASP.NET识别用户
  变种的cu​​rrentUser = manager.FindById(User.Identity.GetUserId());

现在,我们可以访问所有用户的道具(电话号码和语言),如下

  VAR phoneNumber的= currentUser.PhoneNumber;
VAR用户语言= currentUser.Language;

编辑:如果您想要获得相同的任何view.cshtml或_Layout.cshtml那么你应该做像下面

  @using Microsoft.AspNet.Identity
@using Microsoft.AspNet.Identity.EntityFramework
@using YourWebApplication.Models@ {
    VAR phoneNumber的=的String.Empty;
    VAR用户语言=的String.Empty;
    如果(User.Identity.IsAuthenticated){
        变种userStore =新UserStore&所述; ApplicationUser>(新ApplicationDbContext());
        VAR经理=新的UserManager< ApplicationUser>(userStore);
        变种的cu​​rrentUser = manager.FindById(User.Identity.GetUserId());        phoneNumber的= currentUser.PhoneNumber;
        用户语言= currentUser.Language;
    }
}

As I can extend methods to access user properties?

There are methods like:

User.Identity.GetUserId()
User.Identity.GetUserName()

Which are accessible from the views and the controllers.

I want to extend this functionality with methods like:

User.Identity.GetUserPhoneNumber()
User.Identity.GetUserLanguaje()

解决方案

Similar Question: Need access more user properties in User.Indentity answered by Microsoft professionals at codeplex worklog as below

"You can get the User object and do User.Email or User.PhoneNumber since these properties are hanging off the User model"

We can get the application current User object in ASP.Net identity as below, from there you can access all properties of user object, we follow the same in mvc5 web-apps as of now.

//In Account controller like this
var currentUser = UserManager.FindById(User.Identity.GetUserId());

In other controllers you will need to add the following to your controller:

  var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

  // Get the current logged in User and look up the user in ASP.NET Identity
  var currentUser = manager.FindById(User.Identity.GetUserId()); 

Now we can access all user props(Phone# and Language) as below

var phoneNumber = currentUser.PhoneNumber;
var userLanguage = currentUser.Language;

EDIT: If you want to retrieve the same in any view.cshtml or _Layout.cshtml then you should do like below

@using Microsoft.AspNet.Identity
@using Microsoft.AspNet.Identity.EntityFramework
@using YourWebApplication.Models

@{
    var phoneNumber= string.Empty;
    var userLanguage = string.Empty;
    if (User.Identity.IsAuthenticated) {
        var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
        var manager = new UserManager<ApplicationUser>(userStore);
        var currentUser = manager.FindById(User.Identity.GetUserId());

        phoneNumber = currentUser.PhoneNumber;
        userLanguage = currentUser.Language;
    }
}

这篇关于ASP.NET身份扩展的方法来访问用户属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆