我正在使用IdentityServer4来处理ASP.NET Core API中的身份验证和授权。客户端使用Angular4。
我知道可以使用令牌端点(http://myapidomain/connect/token)通过使用grantype =
我的问题是:我们还需要实现API
我的第二个问题是: 当我从
在Angular客户端中。
在我的API服务器端:
ResourceOwnerPassword
来获取access_token
。这意味着我在登录UI中提供username
和password
进行身份验证。我的问题是:我们还需要实现API
Account/Login
吗?我认为IdentityServer4已经自动处理了通过cookie身份验证中间件的登录。
如果我们需要实现API Account/Login
,那么最佳实践是什么?
我在某个地方读到的是使用这个来登录。await HttpContext.Authentication.SignInAsync(identityUser.Id, identityUser.UserName);
这是注销的代码
await HttpContext.Authentication.SignOutAsync
我的第二个问题是: 当我从
connect/token
获取access_token
时,我尝试通过访问http://myapidomain/connect/userinfo获取用户信息。但是我总是得到405错误代码。
我的哪些部分有问题?在Angular客户端中。
authFormHeaders() {
const header = new Headers();
header.append('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
header.append('Accept', 'application/json');
header.append('Authorization', 'Bearer ' + this.oidcSecurityCommon.getAccessToken());
return header;
}
getUserInfo() {
let self = this;
let options = new RequestOptions({
method: RequestMethod.Get,
headers: this.authService.authFormHeaders()
});
return self.http.get(this.authWellKnownEndpoints.userinfoEndpoint, options)
.map((res: Response) => {
return res.json();
})
.catch(self.appService.handleError);
}
在我的API服务器端:
CorsPolicyBuilder corsBuilder = new CorsPolicyBuilder()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowCredentials();
services.AddCors(opts =>
{
opts.AddPolicy("AllowAllOrigins", corsBuilder.Build());
});
var url = optionsAccessor.Value.SystemConfig.Authority;
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = url,
RequireHttpsMetadata = false,
ApiName = "netpower.qms.saas.api"/*,
AllowedScopes = { IdentityServerConstants.StandardScopes.OpenId }*/
});
app.UseCors("AllowAllOrigins");