我试图创建以下结构:
Angular 2客户端应该有Twitter-Sign in按钮。 以下是Twitter的配置:
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
app.UseCors("CorsPolicy");
app.UseIdentity();
app.UseIdentityServer();
//after identity before mvc
app.UseTwitterAuthentication(new TwitterOptions
{
AuthenticationScheme = "Twitter",
DisplayName = "Twitter",
SignInScheme = "Identity.External",
ConsumerKey = "key",
ConsumerSecret = "secret",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
SaveTokens = true,
});
app.UseMvc();
}
此配置在我的数据库中保存了twitter提供的访问令牌和密码。
bool result = false;
var info = await signInManager.GetExternalLoginInfoAsync();
if (info != null)
{
var tempUser = info.Principal;
var claims = tempUser.Claims.ToList();
var userIdClaim = claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
var email = claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email);
if (userIdClaim != null)
{
var isRegistered = await IsUserRegistered(info.LoginProvider, info.ProviderKey);
if (!isRegistered && email != null)
{
var user = new ApplicationUser { UserName = userIdClaim.Value, Email = email.Value };
var userCreated = await userManager.CreateAsync(user);
isRegistered = userCreated.Succeeded;
if (isRegistered)
{
var addLoginresult = await userManager.AddLoginAsync(user, info);
isRegistered = addLoginresult.Succeeded;
if (isRegistered)
{
await signInManager.SignInAsync(user, isPersistent: false);
}
}
}
if (isRegistered)
{
var succeded = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
if (succeded.Succeeded)
{
IdentityResult updateResult = await signInManager.UpdateExternalAuthenticationTokensAsync(info);
result = updateResult.Succeeded;
}
}
}
}
if (!result)
{
await signInManager.SignOutAsync();
}
return Redirect(System.Net.WebUtility.UrlDecode(returnUrl));
我试图找出的是我应该如何实现我的ExternalLoginCallback方法将令牌返回给客户端(Angular或任何其他),以后将用于在WEB API(或多个API)中进行身份验证。现在我可以看到Auth cookie的响应。
答案 0 :(得分:2)
查看这个Damienbod关于使用webapi和Angular2配置IdentityServer4的好例子(已经是Angular4!)https://damienbod.com/2016/03/02/angular2-openid-connect-implicit-flow-with-identityserver4/
如果您使用twitter外部登录或IdentityServer4登录本身,基本上没有区别。您可以删除身份服务器登录表单,只保留Twitter外部登录链接。简而言之,你应该做什么:
配置身份服务器,以便能够通过Twitter http://youridetityserver/Account/Login
使用适当的参数将角度应用中的登录链接设置为http://youridetityserver/connect/authorize。您可以在此处找到参数列表http://docs.identityserver.io/en/release/endpoints/authorize.html
在来自returnURL的angular2 app中提取id_token参数
在angular2应用程序的WebApi服务器调用中设置Bearer标头
设置WebApi以使用IdentityServer授权。