0

データベースユーザーをAzure AD電子メールアドレスで認識し、ローカルデータベースユーザーのプロパティに基づいてAzure AD認証ユーザーにカスタム要求を追加しようとしています。 startup.csで私が得た:Azureユーザー.netコアのローカルデータベースに基づくカスタム権限付与の追加

public void ConfigureServices(IServiceCollection services) 
{ 
    ... 
    services.AddScoped<IClaimsTransformer, ClaimsTransformer>(); 
    ... 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, KayttajatContext context) 
{ 
    ... 
    app.UseClaimsTransformation(async (c) => 
    { 
     IClaimsTransformer transformer = c.Context.RequestServices.GetRequiredService<IClaimsTransformer>(); 
     return await transformer.TransformAsync(c); 
    }); 
    ... 
} 

その後ClaimsTransformer.csは次のようになります。

namespace Authtest 
{ 
    public class ClaimsTransformer : IClaimsTransformer 
    { 

     private readonly KayttajatContext _context; 

     public ClaimsTransformer(KayttajatContext dbContext) 
     { 
      _context = dbContext; 
     } 

     public async Task<ClaimsPrincipal> TransformAsync(ClaimsTransformationContext ctrans) 
     { 

      string sposti = ((ClaimsIdentity)ctrans.Principal.Identity).Name; 
      var user = await _context.Henkilöt.FirstOrDefaultAsync(t => t.Sposti == sposti); 

      if (user.Sposti == sposti) 
      { 
       ((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(new Claim(ClaimTypes.Role, "Administrator")); 
       ((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(new Claim(ClaimTypes.Role, "User")); 
      } 
      else 
      { 
       ((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(new Claim(ClaimTypes.Role, "User")); 
      } 
      return ctrans.Principal; 
     } 
    } 
} 

しかし、これは私に与えます「とNullReferenceException:オブジェクト参照オブジェクトのインスタンスに設定されていません」 at if (user.Sposti == sposti)

代わりに、いずれかの変数にstring値を指定すると、if文が正常に動作します。私は何が間違っているのか分からないのですか?非同期とは何か関係がありますか?これがナットを運転するのを助けてください。

+0

ユーザーオブジェクトがnullのように見えます。非同期ファーストでは、私たちが時間を遡って返されないためです。 –

+0

それは私があまりにも疑わしいことです、どうすればそれが価値を与えることができますか? –

+0

私は非同期プログラミングの貧弱な問題です。それは盲目のショットを完了させるだろう。しかし、まずdbにその電子メールがあることを確認する必要があります。それが確認されたら、非同期について心配する必要があります。 –

答えて

0

設定する前に、文字列sposti = ((ClaimsIdentity)ctrans.Principal.Identity).Name;を呼び出そうとしていました。今すぐ、ありがとう:)

関連する問題