2016-06-19 4 views
1

저는 ASP.NET MVC Core에 익숙하지 않습니다. 제가 원하는 것은 페이스 북 계정으로 사용자를 로그인하는 것입니다. 사용자는 Google 애플리케이션에 로그인하여 클레임 (성과 이름, 이메일 등)을 얻을 수 있습니다. Facebook 로그인을 사용하는 방법을 알고 있습니다.ASP.NET MVC Core (MVC 6) Facebook 로그인 및 클레임 받기

FacebookOptions facebookOptions = new FacebookOptions(); 
facebookOptions.Scope.Add("email"); 
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"]; 
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"]; 
applicationBuilder.UseFacebookAuthentication(facebookOptions); 

applicationBuilder.UseIdentity().UseFacebookAuthentication(); 

나는 그 주장을 어디에서 가져올 지, 그리고 그 코드를 어디에 두어야 하는지를 알아야합니다.

고맙습니다.

+0

그리고? 일을 시작하게 만들었거나, 어디서나 문제를 겪었습니까? 우리가 도울 수 있을지 어쩌면 ... –

+0

우리는 다른 작업 흐름을 결정했습니다 thöx aswer에 – Wasyster

답변

2

신원을 사용하고 있습니다. 하지만 귀하의 질문은 어떻게 페이스 북 계정에서 클레임을 얻는 지에 관한 것이지, 어떻게 유지할 것인가에 관한 것이 아닙니다. 그래서 내가 도울 수 있기를 바란다. 나는 쿠키 미들웨어로 해냈다. ClaimsPrincipal을 Facebook에 로그인하는 것을 잡기 위해 '임시'쿠키 미들웨어를 추가 한 다음 강화 된 ClaimsPrincipal을 유지하기 위해 '진짜'쿠키 미들웨어에 로그인합니다. 시작 프로그램 클래스의 구성 방법에 코드의 관련 부분 다음 facebookOptions의 SignInScheme는 "온도"및 '임시'쿠키 미들웨어의 옵션이 얼마나

  app.UseCookieAuthentication(
      new CookieAuthenticationOptions() 
      { 
       AuthenticationScheme = "Cookie", 
       AutomaticAuthenticate = true, 
       AutomaticChallenge = true, 
       LoginPath = new PathString(@"/account/login"), 
       AccessDeniedPath = new PathString(@"/account/accessdenied") 
      }); 

     app.UseCookieAuthentication(
      new CookieAuthenticationOptions() 
      { 
       AuthenticationScheme = "Temp", 
       AutomaticAuthenticate = false 
      }); 

     var facebookOptions = new FacebookOptions() 
     { 
      AuthenticationScheme = "Facebook", 
      SignInScheme = "Temp", 
      AppId = "yourappidhere", 
      AppSecret = "yourappsecrethere" 
     }; 
     facebookOptions.Scope.Add("public_profile"); 
     facebookOptions.Scope.Add("email"); 

     app.UseFacebookAuthentication(facebookOptions); 

참고가 false로 AutomaticAuthenticate있어이 (당신 때문에 Temp 쿠키에 ClaimsPrinciple을 자동으로 유지하고 싶지는 않지만 여기에 "쿠키"라고하는 실제 쿠키를 풍부하게 유지하고 싶습니다. LoginFacebook이 페이지 또는 무언가의 링크를 통해 호출하는 방법을

 public async Task<IActionResult> Register(string returnUrl = null) 
    { 
     var externalPrincipal = await HttpContext.Authentication.AuthenticateAsync("Temp"); 

     //TODO Check external principal and retrieve claims from db or whatever needs to be done here. 

     var claims = new List<Claim>() 
      { 
       new Claim("email", externalPrincipal.FindFirst(ClaimTypes.Email).Value) 
      }; 
     var id = new ClaimsIdentity(claims, "password"); 
     await HttpContext.Authentication.SignInAsync("Cookie", new ClaimsPrincipal(id)); 
     await HttpContext.Authentication.SignOutAsync("Temp"); 

     return Redirect(returnUrl); 
    } 

    public async Task<IActionResult> LogInFacebook(string returnUrl = null) 
    { 
     var queryString = !string.IsNullOrWhiteSpace(returnUrl) ? $"?returnUrl={returnUrl}" : string.Empty; 
     var props = new AuthenticationProperties() { RedirectUri = [email protected]"Account/Register{queryString}" }; //new PathString(returnUrl) 

     return await Task.Run<ChallengeResult>(() => new ChallengeResult("Facebook", props)); 

    } 

참고 :

은 그럼 내 컨트롤러 관련 방법처럼 보인다. 이 시점에서 Facebook 미들웨어의 SignInScheme이 "임시"상태임을 기억하십시오. '등록'작업 방법으로 리디렉션됩니다. 거기 당신은 코드와 페이스 북에서 ClaimsPrinciple를 추출 :

var externalPrincipal = await HttpContext.Authentication.AuthenticateAsync("Temp"); 

을 당신이 주장와 함께 할 필요가 무엇이든 할 수있는이 시점에서. 나는 당신이 볼 수있는 것처럼 전자 메일 클레임을 추출합니다. 클레임에 ClaimsPrinciple을 유지하기 위해 "쿠키"기호로 로그인합니다.

+0

thnx, 시도해 봅니다 – Wasyster

+0

내 WebAPI에서 페이스 북 로그인을 어떻게 타겟팅 할 수 있습니까? – Wasyster

+0

나는 내가 추측하는 질문을 이해하지 못한다. 원하는 경우 LogInFacebook을 호출 할 수 있습니다. 웹 API 컨트롤러와 MVC 6의 일반 컨트롤러는 차이가 없습니다 ... –

2

Facebook을 사용하여 로그인 한 후에는 AccountControllers ExternalLoginCallback 메소드에서 해당 계정과 관련된 클레임을 검색 할 수 있습니다.

var email = info.Principal.FindFirstValue(ClaimTypes.Email); 
var name = info.Principal.FindFirstValue(ClaimTypes.GivenName) ?? 
         info.Principal.FindFirstValue(ClaimTypes.Name); 
var lastName = info.Principal.FindFirstValue(ClaimTypes.Surname); 
+0

대답은 thnx입니다. – Wasyster