2017-02-16 5 views
0

私は状況があります。現在のユーザーに別のプロパティを取得する方法asp mvc 5

AspNetUsers AccType場合INT型

あるAccTypeと呼ばれる列を持つAccType 2 =マネージャの場合1 =顧客

あります。

私は、次のようなステートメント場合の対処しようとしていますUser.Identity.GetUserId()

によって現在ログインしているユーザーIDにアクセスすることができます。

if(User.Identity.GetUserId().AccType == 1){ 
     redirect to specific page.. 
    }elseif (User.Identity.GetUSerId().AccType == 2){ 
    redirect to 2nd specific page.. 
    } 

User.Identity.GetUserId().AccTypeを、これが今取り組んでいます。

助けてください!ありがとう!

+0

を私はGetUserIdはユーザーIDといないオブジェクトを返すと仮定? – Forklift

+0

なぜこれの代わりに 'Roles'を使用しないのですか? – Valkyrie

+0

私は別の目的を持っているだけなので... –

答えて

0

User.Identity.GetUserId()は、現在ログインしているユーザーのIDを返します。ただし、そのプロパティにアクセスする場合は、実際のユーザーオブジェクトを取得する必要があります。これに対する一つの方法は、以下の通りである。

var userId = this.User.Identity.GetUserId(); 
var user = this.Request.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(userId); 

より便利な方法は、お使いのコントローラにApplicationUserのプロパティを作るために、その後、コードを再利用することになります。

プロパティ:

ユーザーの取得
public ApplicationUserManager UserManager 
{ 
    get 
    { 
     return this.Request.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
    } 
} 

var user = this.UserManager 
    .FindById(this.User.Identity.GetUserId()); 
関連する問題