0

我正在開發一個網站,可以使用Asp.Net WebAPI通過社交網絡進行登錄。 在我的網站中,客戶端部分我使用Facebook登錄SDK,按照Facebook網站上的說明進行操作,並獲得了我的Facebook賬戶。 我寫一個服務(Angular服務),並調用服務器用Facebook用戶ID登錄我的網站。在不調用「令牌」方法的情況下獲取asp網絡中的訪問令牌

function loginExternal(LoginProvider, ProviderKey) 
    { 
     var data = { 
      'LoginProvider':LoginProvider, 
      'ProviderKey':ProviderKey 
     } 
     return $http({ 
      method:'POST', 
      url:url, 
      data:data 
     }); 
    } 

在服務器中,我寫在AccountController.cs一種新方法,它會從客戶端的請求,查詢賬戶,並返回該帳戶的訪問令牌。

// POST API /帳號/ LoginExternal

//POST api/Account/LoginExternal 
      [AllowAnonymous] 
      [Route("LoginExternal")] 
      public async Task<IHttpActionResult> LoginExternal(UserLoginInfoViewModel model) 
      { 
       ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(model.LoginProvider, 
        model.ProviderKey)); 

       bool hasRegistered = user != null; 

       if (hasRegistered)//has the account in database 
       { 
        Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); 

        ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, 
         OAuthDefaults.AuthenticationType); 
        ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager, 
         CookieAuthenticationDefaults.AuthenticationType); 

        AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user); 

        Authentication.SignIn(properties, oAuthIdentity, cookieIdentity); 
       } 
       else //dont have the account database - not implemented 
       { 
       } 
       return Ok(); 
      } 

在這一點上,我可以檢查是否在數據庫中存在的賬戶。但是,我不知道如何在這個方法中返回與這個賬戶相對應的access_token?以前,當我想登陸本地帳戶,我得打電話給服務器

本地主機:8080 /令牌

,並通過賬戶名和密碼,響應將返回的access_token。但我怎麼用這個方法呢?

回答

0

我想我找到了解決方案。請看我的答案。 :)

//POST api/Account/LoginExternal 
     [AllowAnonymous] 
     [Route("LoginExternal")] 
     public async Task<IHttpActionResult> LoginExternal(UserLoginInfoViewModel model) 
     { 
      ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(model.LoginProvider, 
       model.ProviderKey)); 

      bool hasRegistered = user != null; 

      if (hasRegistered)//has the account in database 
      { 
       Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); 

       ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, 
        OAuthDefaults.AuthenticationType); 
       ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager, 
        CookieAuthenticationDefaults.AuthenticationType); 

       AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user); 
       //Create an access_token with expire time 14 days 
       AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties()); 
       DateTime currentUtc = DateTime.UtcNow; 
       ticket.Properties.IssuedUtc = currentUtc; 
       ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(14)); 
       string accessToken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket); 

       Authentication.SignIn(properties, oAuthIdentity, cookieIdentity); 
       return Ok(accessToken);//Return Access_token to client 
      } 
      else //dont have the account database - not implemented 
      { 

      } 

     }