2013-12-11 42 views
0

User.Identity當然可以在控制器中訪問。但我不想讓每個控制器都有複製的方法來獲取用戶的常用屬性。User.Identity和相關屬性 - 我如何使用可以由我的任何控制器調用的單一方法處理它們?

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

什麼是這樣做的好方法,以便它可以被我的所有控制器訪問?我會把它放在哪裏,我怎麼稱呼它,以及我需要通過哪種方式來通過它才能使其工作,以最佳實踐的方式進行工作?

+0

我最初提出這個問題,包括在標題中,關於在由我的不同控制器調用的單個方法中設置ViewBag變量。今天我的研究表明,ViewBag變量不能從創建它們的控制器類訪問。我最初的帖子的實際意圖是能夠訪問來自我的不同控制器的單個類的User.Identity信息。 – Reid

回答

0

試試這個..ovveride您的控制器與基本控制器

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

控制器

public class HomeController : BaseController 
    { 
     //Here you can directly use User.UserId or User.favouriteColor 
    } 
+0

謝謝。我已經試過了,並且在瀏覽器中出現黃頁錯誤:「System.NullReferenceException:對象引用未設置爲對象的實例。」事實上,如果我清除Initialize覆蓋內的代碼,我會得到相同的錯誤。另外,公共YOUR_MODEL用戶{get;組; }行似乎是不必要的,沒有用,因爲它是我想使用的現有的IPrincipal用戶,雖然我可能在這裏錯過了你的觀點。無論如何,謝謝你的想法,這似乎很聰明。請讓我知道如果我在這裏錯過了一些觀點。 – Reid

+0

我會注意到它在離開了重載初始化代碼後發生了爆炸。對我來說好奇的是,如果我在代碼中使用User.Identity.IsAuthenticated而不是Thread.CurrentPrincipal.Identity.IsAuthenticated,它就會暴露出來。 – Reid

0

我的目標是獲得從一個類User.Identity信息的幾個控制器之外我有使該代碼不會被複制。

我只是想出瞭如下(isApproved,isLockedOut和MEMBERID領域是我自己的條目進AspNetUsers表):

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. 

然後我選擇了第二行並做了重構;提取方法。結果是我學會了要傳遞什麼類型以及如何調用它。

相關問題