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
});
})...
看起来很安全,但了解我不是安全专家所以这段代码没有保修,如果你看到任何明显的东西请告诉我,我会更新代码. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 无法转换类型为’System.Web.UI.LiteralControl
- 编辑并在ASP.NET Web项目中继续
- asp.net – Autofac懒惰属性注入
- 在ASP.NET中使用数据源控件真的很专业吗?
- asp.net gridview复选框选择
- asp.net – 替换过时的System.Xml.XmlDataDocument?
- asp.net-mvc – 在ajax请求中返回PDF
- asp.net-mvc – 将JWT令牌存储在cookie中
- asp.net-mvc-3 – MVC 3 knockoutjs:在使用EditorFor作为布
- asp.net-mvc-3 – 是否需要StructureMap HttpContextScoped
- asp.net – 是否允许使用manifest.json的相对路径
- 如何在服务器端缓存ASP.NET自定义HttpHandler响应
- asp.net-mvc – 什么原因导致asp.net无法创建/影
- asp.net-mvc – ASP.NET MVC API或WCF API
- asp.net-mvc – 映射从域实体到DTO的验证属性
- 发布ASP.NET应用程序时,是否应该将构建类型更改为
- asp.net – 按Enter键时提交表单
- asp.net – 来自WebHttpBinding的WCF服务中的Acc
- asp.net – 每个人如何存储连接字符串?
- .net – 什么可以解释托管堆上超过5,000,000个Sy
