2016-04-28 11 views
1

MVCアプリケーションでは、TPT inheritanceを使用して基本ASP ID ApplicationUserクラスの2つのサブクラスを作成し、いくつかのクレームをオブジェクトに追加して、ビューのサブクラス。ApplicationUserのサブクラスにクレームを追加する

私は単純なトリックを欠いている/ ASPアイデンティティセットアップの基本的な誤解を持っている必要がありますが、私はこれを行う方法がわかりません。

ApplicationUserクラスにクレームを追加することは簡単ですが、これを行うGenerateUserIdentityAsyncメソッドをサブクラスでオーバーライドしてそこで実行することはできません。

これを実現する手段はありますか(この設定で他のものはうまく動作しています)、またはサブクラスを2つ設定してIdentityUserから直接継承し、2つの設定をそれらの両方でIdentityConfig.cs?あなたはGenerateUserIdentityAsync、あなたの具体的な種類の実装をオーバーライドできるようになるApplicationUserで仮想メソッドにすることができ

//The ApplicationUser 'base' class 
public class ApplicationUser : IdentityUser 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string ProfilePicture { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 

     // Add custom user claims here 

     //** can add claims without any problems here ** 
     userIdentity.AddClaim(new Claim(ClaimTypes.Name, String.Format("{0} {1}", this.FirstName, this.LastName)));I 

     return userIdentity; 
    } 
} 

public class MyUserType1 : ApplicationUser 
{ 
     [DisplayName("Job Title")] 
     public string JobTitle { get; set; } 

     //** How do I add a claim for JobTitle here? ** 
} 


public class MyUserType2 : ApplicationUser 
{ 
     [DisplayName("Customer Name")] 
     public string CustomerName { get; set; } 

     //** How do I add a claim for CustomerName here? ** 
} 

答えて

3

次のように

私が話しているクラスがあります。

これは私が見ることができる最もクリーンなオプションです。

+0

基本クラスでは利用できない2つのサブクラス内のプロパティの主張を追加したいと思います。 – Ted

+1

はい、私はあなたが意味するものを得ます。基本クラスでGenerateUserIdentityAsyncを仮想にして、具象クラスでオーバーライドできますか?それを抽象的なものにすることもできますが、ApplicationUserはIdentityUser –

+1

Brilliantを実装しているため、これを行うことはできません。もちろんそうです。 GenerateUserIdentityAsync'メソッドがIdentityUserインターフェイスの一部であり、変更できなかったことは私の頭の中にありました。私はこの方法で仮想修飾子を使用していないことを認めています。とにかくこれを考えて、多くの感謝! (代わりにこれを提案するためにあなたの答えを編集したい場合、私はそれを受け入れupvote) – Ted

関連する問題