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 
      { 

      } 

     }