在使用Identity Server 4和ASP.NET Identity添加身份驗證功能後,我打算添加Google Provider,以便用戶還可以使用其Google +帳戶登錄。我使用Angular作爲我的前端,並使用ASP.NET Web Api(Core)作爲後端。使用Identity Server 4和ASP.NET Identity添加外部登錄
// Login client
public login(email: string, password: string): Observable<any> {
let body: any = this.encodeParams({ /* cliend_id, grant_type, username, password, scope */ });
return this.http.post("http://localhost:64023/connect/token", body, this.options)
.map((res: Response) => {
const body: any = res.json();
if (typeof body.access_token !== "undefined") {
// Set localStorage with id_token,..
}
}).catch((error: any) => { /**/);
}
// Register Web API
[HttpPost("Create")]
[AllowAnonymous]
public async Task<IActionResult> Create([FromBody]CreateUserViewModel model)
{
var user = new ApplicationUser
{
FirstName = model.FirstName,
LastName = model.LastName,
AccessFailedCount = 0,
Email = model.Email,
EmailConfirmed = false,
LockoutEnabled = true,
NormalizedEmail = model.Email.ToUpper(),
NormalizedUserName = model.Email.ToUpper(),
TwoFactorEnabled = false,
UserName = model.Email
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await addToRole(model.Email, "user");
await addClaims(model.Email);
}
return new JsonResult(result);
}
// Identity Server Startup
app.UseGoogleAuthentication(new GoogleOptions
{
AuthenticationScheme = "Google",
DisplayName = "Google",
SignInScheme = "Identity.External",
// ClientId, ClientSecret,..
});
用戶登錄後,localStorage被設置,我可以保護安全控制器。對於谷歌提供我增加了一個額外的按鈕,下面的方法:
initGoogleAPI() {
let self = this;
gapi.load('auth2', function() {
self.auth2 = gapi.auth2.init({ /* client_id, cookiepolicy, scope */ });
self.externalLogin(document.getElementById('google-button'));
});
}
externalLogin(element) {
let self = this;
this.auth2.attachClickHandler(element, {},
function (googleUser) {
// retrieved the id_token, name, email,...
}, function (error) {
alert(JSON.stringify(error, undefined, 2));
});
}
我已經找到了幾個解決方案,但僅適用於MVC應用程序而不是使用客戶方框架SPA。接下來需要採取哪些步驟才能使外部登錄工作?當用戶首次使用外部提供商登錄時,是否需要在AspNetUsers表中創建新記錄?
不應該登錄由供應商來處理?選擇一個谷歌帳戶後,我從提供者生成的響應中檢索一個令牌,而不是由Identity Server生成。我已經按照您向我發送的指南進行了操作,但不幸的是,它沒有提供任何有關將外部提供者添加到與ASP.NET身份相結合的SPA的信息。 – Sam
您正在從Identity Server和Google接收令牌,具體取決於登錄類型,它們可能不包含您需要的聲明。如果您的身份服務器允許Google登錄,那麼您的應用程序只會從Identity Server獲取一種令牌類型。 Identity Server仍然會將您傳遞給Google進行身份驗證,但它會在返回到您的應用程序之前返回到Identity Server。 –