Asp.net mvc 5 身份和相关属性-我如何在一个可以被我的任何控制器调用的方法中处理它们?

Asp.net mvc 5 身份和相关属性-我如何在一个可以被我的任何控制器调用的方法中处理它们?,asp.net-mvc-5,asp.net-identity,Asp.net Mvc 5,Asp.net Identity,当然,身份可以在控制器中访问。但我不想让每个控制器都有一个复制的方法来获取用户的公共属性 public class myClass { public void myMethod() { //assume user is authenticated for the purposes of this question var currentUserId = User.Identity.GetUserId(); var manage

当然,身份可以在控制器中访问。但我不想让每个控制器都有一个复制的方法来获取用户的公共属性

public class myClass
{
  public void myMethod()
  {    //assume user is authenticated for the purposes of this question 
        var currentUserId = User.Identity.GetUserId();           
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
        var currentUser = manager.FindById(currentUserId);
        ViewBag.UserID = currentUser.Id;
        ViewBag.favoriteColor = currentUser.favoriteColor;            
  }
}
公共类myClass
{
公共方法()
{//假设用户已为此问题进行了身份验证
var currentUserId=User.Identity.GetUserId();
var manager=newusermanager(newuserstore(newapplicationdbcontext());
var currentUser=manager.FindById(currentUserId);
ViewBag.UserID=currentUser.Id;
ViewBag.favoriteColor=currentUser.favoriteColor;
}
}

这样做的好方法是什么,以便我的所有控制器都可以访问它?我应该把它放在哪里,怎么称呼它,我可能需要通过什么才能使它以最佳实践的方式工作?

试试这个..用基本控制器验证你的控制器

  public class BaseController : Controller
    {
       public YOUR_MODEL User{ get; set; }
       protected override void Initialize(RequestContext requestContext)
        {
          if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
              {
                var currentUserId = User.Identity.GetUserId();           
                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                var currentUser = manager.FindById(currentUserId);
                User.UserID = currentUser.Id;
                User.favoriteColor = currentUser.favoriteColor;     
              }
        }
   }

我的目标是从我拥有的几个控制器之外的类中获取User.Identity信息,这样代码就不会被复制

我刚刚想到的是以下内容(isApproved、isLockedOut和memberID字段是我自己在AspNetUsers表中的条目):


获取预期的参数类型是我最初的问题。但后来我尝试并创建了以下代码:

var myUser = User;
var currentUserId = myUser.Identity.GetUserId();//Refactor: Extract Method for this line.

然后我选择了第二行并进行了重构;提取方法。结果我学会了通过哪种考试以及如何称呼它。

谢谢。我尝试了这个方法,但在浏览器上出现了一个黄页错误:“System.NullReferenceException:对象引用未设置为对象的实例。”事实上,如果我清除初始化覆盖中的代码,我会收到相同的错误。此外,您的公共模型用户{get;set;}行似乎没有必要,也没有用处,因为我想使用的是现有的IPrincipal用户,尽管我可能没有理解您的观点。无论如何,谢谢你的想法,这似乎很聪明。请让我知道我是否遗漏了这里的某一点。我会注意到爆炸发生在它离开覆盖初始化代码之后。令我好奇的是,如果我在代码中对User.Identity.IsAuthenticated进行了验证,而不是对Thread.CurrentPrincipal.Identity.IsAuthenticated进行了验证,那么它就在这一点上爆炸了。我最初提出了这个问题,包括在标题中,关于在由不同控制器调用的单个方法中设置ViewBag变量。我今天的研究表明,ViewBag变量不能从创建它们的控制器类之外访问。我最初发表文章的真正目的是能够从我的不同控制器访问单个类中的User.Identity信息。
public class MyClass
{
   public string getMemberID(System.Security.Principal.IPrincipal User)//Note that this is the same name as is passed in, just personal preference       
    {

        if (User.Identity.IsAuthenticated)
        {
            var currentUserId = User.Identity.GetUserId();
            if (currentUserId == null)
            {
               return "0";
            }
            else
            {
                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                var currentUser = manager.FindById(currentUserId);

                if (currentUser.isApproved && !currentUser.isLockedOut)
                    return  currentUser.memberID.ToString();
                else
                   return "0";
            }
        }
        else
            return "0";
    }
}
 string memberID =   MyClass.getMemberID(User);
var myUser = User;
var currentUserId = myUser.Identity.GetUserId();//Refactor: Extract Method for this line.