【发布时间】:2016-06-01 22:57:24
【问题描述】:
我正在开发一个可以使用 Asp.Net WebAPI 通过社交网络登录的网站。 在我的网站中,我使用 Facebook 登录 SDK 的客户端部分,按照 Facebook 网站中的说明获取我的 Facebook 帐户。 我编写了一个服务(Angular 服务)并调用服务器以使用 Facebook 用户 ID 登录我的网站。
function loginExternal(LoginProvider, ProviderKey)
{
var data = {
'LoginProvider':LoginProvider,
'ProviderKey':ProviderKey
}
return $http({
method:'POST',
url:url,
data:data
});
}
在服务器中,我在 AccountController.cs 中编写了一个新方法,它将接受来自客户端的请求,检查帐户并返回该帐户的访问令牌。
//POST api/Account/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?以前,当我想登录本地帐户时,我必须调用服务器
localhost:8080/令牌
并通过账户名和密码,响应将返回access_token。但是我该如何使用这种方法呢?
【问题讨论】:
标签: asp.net angularjs asp.net-web-api facebook-login access-token