2016-08-08 6 views
0

내가하려는 것은 ADFS 로그인에서 반환하는 사용자 클레임에 액세스하는 것입니다. ADFS는 사용자 이름을 반환하고 사용자 이름을 얻고 저장하기 위해 다른 사용자 이름으로 쿼리를 실행해야합니다. 나는 그 일을하는 곳과 최선의 실천이 무엇인지를 정말로 모른다. 내가 좋아하는 뷰 컨트롤러에서 사용자 요구에 액세스 할 수 있습니다외부 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