2015-02-03 2 views
0

솔루션에 싱글 사인을 개발 중입니다. 사용자가 Google 서비스에 액세스 할 수 있도록 사이트를 방문 할 때마다 사용자를 Google 계정에 로그인 할 수 있어야합니다. 나는 구글에서 액세스 및 새로 고침 토큰을 받고 내 데이터베이스에 저장하지만 난에 사용자가 로그인 할 때이 토큰 무엇을 모르는입니다.내 사이트를 방문 할 때 어떻게 사용자를 Google에 로그인 할 수 있습니까?

 public async Task<ActionResult> IndexAsync(CancellationToken cancellationToken) 
    { 
     flowData = new AppFlowMetadata(); 
     UsersSSOTokens userToken = GetCurrentUserToken(); 

     if (userToken != null) 
     { 
      CheckTokenValid(userToken); 
      LogIntoGoogleWithToken(); 
     } 
     else 
      if (result == null || result.Credential == null) 
      { 
       result = await new AuthorizationCodeMvcApp(this, flowData). 
        AuthorizeAsync(cancellationToken); 

       if (result.Credential == null) return new RedirectResult(result.RedirectUri); 
      } 

     return View(); 
    } 

     public async Task<ActionResult> GetResult(string code, string error, string state) 
    { 
     var returnUrl = Request.Url.ToString(); 
     returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?")); 
     var userId = Session["user"]; 

     var token = await flowData.Flow.ExchangeCodeForTokenAsync(userId.ToString(), code, returnUrl, 
      CancellationToken.None); 

     if (token != null && error == null) 
     { 
      if (token.AccessToken != null && token.RefreshToken != null) 
       SaveToken(token.AccessToken, token.RefreshToken, token.TokenType, token.Issued, token.Scope); 
     } 

     return new RedirectResult(state); 
    } 

답변

0

당신이 액세스/새로 고침 토큰을 가질 때, 사용자가 실제로 로그인했습니다. 로그 아웃/로그인은 대부분 클라이언트 측 개념입니다. 서버 측에서는 앱 대신 사용자를 대신하여 API 호출을하도록 승인 된 후에해야하는 일은 API 호출을 만드는 것입니다.

TokenResponse token = new TokenResponse 
{ 
    AccessToken = this.GoogleAccessToken, 
    RefreshToken = this.GoogleRefreshToken 
}; 

IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
    new GoogleAuthorizationCodeFlow.Initializer 
    { 
     ClientSecrets = PlusHelper.GetClientConfiguration().Secrets, 
     Scopes = new string[] { PlusService.Scope.PlusLogin } 
    }); 

UserCredential credential = new UserCredential(flow, "me", token); 
bool success = credential.RefreshTokenAsync(CancellationToken.None).Result; 

PlusService plusService = new PlusService(
    new Google.Apis.Services.BaseClientService.Initializer() 
    { 
     ApplicationName = "Haikunamata", 
     HttpClientInitializer = credential 
    }); 

Person me = await plusService.People.Get(@"me").ExecuteAsync(); 
+0

API 액세스가 필요하지 않습니다. 나는 사용자가 Google에 로그인해야합니다. –

관련 문제