微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET Web API 2:通过本机移动(iOS)应用程序与外部提供程序登录

我已经做了很多搜索,但却找不到理想的解决方案.我知道有一个所谓的解决方案( WebApi ASP.NET Identity Facebook login)但是,解决方案的一些元素是(在我看来)严重hacky(例如用普通帐户注册用户然后添加外部登录,而不是在外部注册它们登录).

我希望能够在已经在iOS移动应用程序上使用Facebook SDK登录注册和验证ASP.NET Web API 2应用程序,即我已经使用他们的SDK对Facebook进行了身份验证,现在想要无缝地使用ASP.NET Web API注册/验证.我不想使用我必须使用Web调用(/ api / Account / ExternalLogin)的过程,因为这对本机移动应用程序来说不是一个很好的用户体验.

我试过了解OWIN,但.NET框架很复杂,我在如何解决这个问题上苦苦挣扎.

解决方法

我今天需要为我的Ionic app做这个. Web API帐户控制器对如何做事有自己的看法,最好的理解方法是阅读Dominick Baier撰写的这篇非常棒的3部分博客文章. https://leastprivilege.com/2013/11/26/dissecting-the-web-api-individual-accounts-templatepart-3-external-accounts/.

解决这个问题的方法是忘记开箱即用的流程,而是使用本地Facebook登录中的accesstoken,然后调用以下服务器代码1)调用Facebook API来验证访问令牌,2)从Facebook调用,获取电子邮件和ID,3)获取用户或创建它(和登录),这已经是其他地方的帐户控制器中的代码,4)为后续Web API创建本地权限JWT调用.

public class ProviderAndAccesstoken {
    public string Token { get; set; }
    public string Provider { get; set; }
}

[AllowAnonymous]
[HttpPost]
[Route("JwtFromProviderAccesstoken")]
public async Task<IHttpActionResult> JwtFromProviderAccesstoken(ProviderAndAccesstoken model) {
    string id = null;
    string userName = null;

    if (model.Provider == "Facebook") {
        var fbclient = new Facebook.FacebookClient(model.Token);
        dynamic fb = fbclient.Get("/me?locale=en_US&fields=name,email");
        id = fb.id;
        userName = fb.email;
    }

    //Todo: Google,LinkedIn

    ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(model.Provider,id));
    bool hasRegistered = user != null;

    string accesstoken = null;
    var identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
    var props = new AuthenticationProperties() {
        IssuedUtc = DateTime.UtcNow,ExpiresUtc = DateTime.UtcNow.Add(Startup.OAuthOptions.AccesstokenExpireTimeSpan),};

    if (hasRegistered) {
        ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,OAuthDefaults.AuthenticationType);
        ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,CookieAuthenticationDefaults.AuthenticationType);

        AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
        Authentication.SignIn(properties,oAuthIdentity,cookieIdentity);


        identity.AddClaim(new Claim(ClaimTypes.Name,user.UserName));
        identity.AddClaim(new Claim("role","user"));

    }
    else {

        user = new ApplicationUser() { UserName = userName,Email = userName };

        IdentityResult result = await UserManager.CreateAsync(user);
        if (!result.Succeeded) {
            return GetErrorResult(result);
        }

        result = await UserManager.AddLoginAsync(user.Id,new UserLoginInfo(model.Provider,id));
        if (!result.Succeeded) {
            return GetErrorResult(result);
        }

        identity.AddClaim(new Claim(ClaimTypes.Name,userName));
    }

    identity.AddClaim(new Claim("role","user"));
    var ticket = new AuthenticationTicket(identity,props);
    accesstoken = Startup.OAuthOptions.AccesstokenFormat.Protect(ticket);

    return Ok(accesstoken);
}

我在Ionic中使用的代码基本上是为了从Facebook获取访问令牌,然后调用Web API以获取本地权限JWT用作Bearer令牌.

Facebook.login(['public_profile','email']).then((result) => {
    return this.http.post("<URL>/api/Account/JwtFromProviderAccesstoken",{ provider: "Facebook",token: result.authResponse.accesstoken })
        .map((res: Response) => res.json())
        .catch(this.handleError)
        .subscribe((res: Response) => {
            // use the result as the Bearer token
        });
})...

看起来很安全,但了解我不是安全专家所以这段代码没有保修,如果你看到任何明显的东西请告诉我,我会更新代码.

原文地址:https://www.jb51.cc/aspnet/248117.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐