从cookie社交登录转移到令牌社交登录

时间:2016-08-30 22:41:54

标签: asp.net owin

这是我在程序员StackExchange上发布的a question的后续内容,我没有回答。也许它位置不当,应该在这里。但无论哪种方式,我都会捶打更多的东西,切碎并改变位数,现在我更接近,但需要更多的帮助。

我尝试使用Owin和各种Katana库来实现Facebook登录(以及后来的其他人)。我只做登录,没有注册,没有ASP.NET DB。我没有使用ASP.NET标识(​​就像在个人帐户中使用默认的ASP.NET项目一样),这是90%的教程告诉你要做的事情。抱歉,如果我听起来很沮丧。我已经设法找到其他人设法打破模具并实施OWIN没有身份gubbins并且我已经做了相当多的阅读。我想我需要帮助连接最后几个点。

这适用于后端带有Web API的SPA。所以我自然而然地倾向于持有OAuth令牌。

我使用this blog post的一些指导,设法让cookie以cookie方式工作。我模仿它的方法,现在我可以用Facebook登录,当回调动作被击中时,User.Identity被成功设置为我的Facebook声明身份。

问题是,对于我的Web API,我希望通过auth / facebook或其他方式调用我的身份的OAuth持有者令牌给用户以用于将来的请求,而不是cookie。

我最好以某种方式从这个cookie转到令牌吗?如何?或者有更好的方法吗?

这是我的Startup.cs代码

public class Startup
{
    // public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }

    public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);

        // other standard stuff (I think)
    }

    private void ConfigureOAuth(IAppBuilder app)
    {
        // lots of commented out code of me trying to do this using a custom provider and OAuth

        var cookieOptions = new CookieAuthenticationOptions
        {
            LoginPath = new PathString("api/auth/facebook")
        };

        app.UseCookieAuthentication(cookieOptions);
        app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType);

        app.UseFacebookAuthentication(new FacebookAuthenticationOptions
        {
            AppId = fbAppID,
            AppSecret = fbAppSecret,
        });
    }
}

这是我的auth控制器代码。

[RoutePrefix("api/auth")]
public class AuthController : ApiController
{
    [AllowAnonymous]
    [Route("facebook")]
    public IHttpActionResult GetFacebookChallenge()
    {
        return new ChallengeResult("Facebook", "api/auth/callback", this.Request);
    }

    [Route("callback")]
    [HttpGet]
    public IHttpActionResult Callback()
    {
        var info = Request.GetOwinContext().Authentication.GetExternalLoginInfo();
        return Ok(info);
    }
}

目前,回调操作返回null,因为info为null,但User.Identity设置了正确的声明,一切都很好。之前的实现使其处于回调不会对User.Identity进行身份验证的状态,但它可以检索外部登录声明身份。但同样,我无法通过身份验证并从中获取令牌。我接近这种方法了吗?

很抱歉这个问题很长。如果您需要我发布更多我的代码,请告诉我,为了简洁,我试图减少它。

0 个答案:

没有答案
相关问题