Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在asp net中获取访问令牌而不调用;代币;方法_Asp.net_Angularjs_Asp.net Web Api_Facebook Login_Access Token - Fatal编程技术网

Asp.net 在asp net中获取访问令牌而不调用;代币;方法

Asp.net 在asp net中获取访问令牌而不调用;代币;方法,asp.net,angularjs,asp.net-web-api,facebook-login,access-token,Asp.net,Angularjs,Asp.net Web Api,Facebook Login,Access Token,我正在开发一个可以使用Asp.NETWebAPI通过社交网络登录的网站。 在我的网站中,客户端部分我使用Facebook登录SDK,按照Facebook网站中的说明操作,并获得我的Facebook帐户。 我编写了一个服务(Angular服务),并调用服务器使用Facebook用户ID登录我的站点 function loginExternal(LoginProvider, ProviderKey) { var data = { 'LoginProvi

我正在开发一个可以使用Asp.NETWebAPI通过社交网络登录的网站。 在我的网站中,客户端部分我使用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中编写了一个新方法,它将接收来自客户端的请求,检查帐户并返回该帐户的访问令牌

//发布api/帐户/登录内部

//发布api/Account/LoginExternal
[异名]
[路线(“LoginExternal”)]
公共异步任务LoginInternal(UserLoginInfoViewModel模型)
{
ApplicationUser user=wait UserManager.FindAsync(新用户登录信息(model.LoginProvider,
model.ProviderKey);
bool hasRegistered=user!=null;
if(hasRegistered)//在数据库中有帐户
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity oAuthIdentity=等待用户.GenerateUserIdentityAsync(UserManager,
OAuthDefaults.AuthenticationType);
ClaimSideEntity cookieIdentity=等待用户.GenerateUserIdentity异步(UserManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties=ApplicationAuthProvider.CreateProperties(用户);
身份验证.签名(属性、oAuthIdentity、cookieIdentity);
}
else//没有帐户数据库-未实现
{
}
返回Ok();
}
此时,我可以检查数据库中是否存在该帐户。但是,我不知道如何在此方法中返回此帐户对应的access_令牌?以前,当我想登录本地帐户时,我必须调用服务器

本地主机:8080/Token


并通过帐户名和密码,响应将返回access\u令牌。但是我怎么能用这种方法呢?

我想我找到了解决办法。请看我的答案。:)

//发布api/Account/LoginExternal
[异名]
[路线(“LoginExternal”)]
公共异步任务LoginInternal(UserLoginInfoViewModel模型)
{
ApplicationUser user=wait UserManager.FindAsync(新用户登录信息(model.LoginProvider,
model.ProviderKey);
bool hasRegistered=user!=null;
if(hasRegistered)//在数据库中有帐户
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity oAuthIdentity=等待用户.GenerateUserIdentityAsync(UserManager,
OAuthDefaults.AuthenticationType);
ClaimSideEntity cookieIdentity=等待用户.GenerateUserIdentity异步(UserManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties=ApplicationAuthProvider.CreateProperties(用户);
//创建过期时间为14天的访问令牌
AuthenticationTicket=newauthenticationticket(oAuthIdentity,newauthenticationProperties());
DateTime currentUtc=DateTime.UtcNow;
ticket.Properties.IssuedUtc=currentUtc;
ticket.Properties.ExpiresUtc=currentUtc.Add(TimeSpan.FromDays(14));
字符串accessToken=Startup.OAuthOptions.AccessTokenFormat.Protect(票证);
身份验证.签名(属性、oAuthIdentity、cookieIdentity);
return Ok(accessToken);//将访问令牌返回给客户端
}
else//没有帐户数据库-未实现
{
}
}
//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();
            }
   //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
            {

            }

        }