2016-08-08 7 views
0

私がやっていることは、ADFSログインから返されるユーザークレームにアクセスすることです。 ADFSはユーザー名を返し、そのユーザー名で別のDBにクエリを実行してユーザー情報を取得して保存する必要があります。私はそれをどこで行うべきか、ベストプラクティスが何であるか本当に分かりません。私は次のようにビューコントローラでユーザーの請求にアクセスすることができます。外部ADFSログインにページが読み込まれる前にユーザークレームを取得

public ActionResult Index() 
{ 
    var ctx = Request.GetOwinContext(); 
    ClaimsPrincipal user = ctx.Authentication.User; 
    IEnumerable<Claim> claims = user.Claims; 
    return View(); 
} 

しかし、私はglobal.asax.csまたはstartup.csのようにアクセスクレームがアプリケーションを実行する前にユーザー情報を保存するために言ったように私は何をする必要があることです。

これは私のStartup.Auth.csファイルです:

public partial class Startup 
{ 
    private static string realm = ConfigurationManager.AppSettings["ida:Wtrealm"]; 
    private static string adfsMetadata = ConfigurationManager.AppSettings["ida:ADFSMetadata"]; 

    public void ConfigureAuth(IAppBuilder app) 
    { 
     app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType); 

     app.UseCookieAuthentication(
      new CookieAuthenticationOptions 
      { 
       AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType 
      }); 

     app.UseWsFederationAuthentication(
      new WsFederationAuthenticationOptions 
      { 
       Wtrealm = realm, 
       MetadataAddress = adfsMetadata 
      }); 
    } 
} 

答えて

1

当社は、起動ファイルでWsFederationAuthenticationOptions値にイベントハンドラを追加します。

これは、セキュリティトークンが検証された直後に発生します。

app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions() 
{ 
    MetadataAddress = MetadataAddress, 

    Wtrealm = Wtrealm, 
    Wreply = CallbackPath, 
    Notifications = new WsFederationAuthenticationNotifications() 
    { 
     SecurityTokenValidated = (ctx) => 
     { 
      ClaimsIdentity identity = ctx.AuthenticationTicket.Identity; 
      DoSomethingWithLoggedInUser(identity); 
     } 
    } 
}; 
+0

ユーザー情報を取得するのにはちょっとした作業がありますが、もう別の問題があります。認証は生涯のように思えます。サーバーに接続しようとすると、私はとにかくログインしていて、ADFSの認証にリダイレクトしていないと思います。 CookieAuthenticationDefaultsをWsFederationAuthenticationDefaultsに変更して、私のコードで何が間違っているのかも知れないと思いますか? – elly

+0

申し訳ありませんが、私の悪い、何かビルドに間違っていた。とにかくこれで解決しました!ありがとうございました。 – elly

関連する問題