【问题标题】:Get the access token in asp net without calling "Token" method在不调用“令牌”方法的情况下获取asp net中的访问令牌
【发布时间】: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


    【解决方案1】:

    我想我找到了解决方案。请看我的回答。 :)

       //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
                {
    
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 2013-08-15
      • 2022-11-24
      • 1970-01-01
      • 2012-04-30
      相关资源
      最近更新 更多