我无法理解两者之间的区别,asp.net identy基于OWIN,并且在IdentityServer介绍他时没有引入中间件,我很困惑..
我一直致力于迁移单片ASP Core MVC应用程序以使用服务架构设计.MVC前端网站使用HttpClient从ASP Core Web API加载必要的数据.前端MVC应用程序的一小部分还需要使用IdentityServer4(与后端API集成)进行身份验证.这一切都很有效,直到我Authorize在Web API上的控制器或方法上放置了一个属性.我知道我需要以某种方式将用户授权从前端传递到后端才能使其正常工作,但我不确定如何工作.我试过获取access_token:User.FindFirst("access_token")但它返回null.然后我尝试了这个方法,我可以获得令牌:
var client = new HttpClient("url.com");
var token = HttpContext.Authentication.GetTokenAsync("access_token")?.Result;
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
Run Code Online (Sandbox Code Playgroud)
此方法获取令牌但仍未使用后端API进行身份验证.我对这个OpenId/IdentityServer概念很陌生,任何帮助都将不胜感激!
以下是MVC Client Startup类的相关代码:
private void ConfigureAuthentication(IApplicationBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies",
AutomaticAuthenticate = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
Authority = "https://localhost:44348/",
RequireHttpsMetadata = false,
ClientId = "clientid",
ClientSecret = "secret",
ResponseType = "code id_token",
Scope = { "openid", "profile" …Run Code Online (Sandbox Code Playgroud) 我遇到的问题是我的IdentityServer的/ connect/introspect端点有时真的很慢(一次调用10秒).如下所示,大多数呼叫(18k)快速执行(<250ms).
我启用了新的Application Insights分析,大多数慢速跟踪如下所示:
正如Application Insights剖析器页面上所述:
BLOCKED_TIME表示代码正在等待另一个资源可用,例如等待同步对象,等待线程可用或等待请求完成.
但我没有理由相信这应该等待一些事情.请求中没有高峰,因此我认为没有可用的线程或其他东西.内存似乎对我们的应用服务计划没有问题,因为它总是大约40%.我也挖掘了IdentityServer4的来源,但无法确定任何原因.所以现在我有点卡住了.有人能指出这些缓慢请求的可能原因吗?或者指出我确定原因的好方向?任何帮助都感激不尽!
使用一些额外信息进行编辑:我们使用IdentityServer4.EntityFramework在sql azure db中存储引用令牌.我查看了Application Insights,这些慢速请求中的查询执行时间不到50毫秒.所以我猜它不是数据库.
c# profiler json.net azure-application-insights identityserver4
目前,我已经创建了一个 Identity server 4 Web 应用程序,其中包含具有默认客户端 ID 和机密的外部登录提供程序。但我的目标是基于租户注册 Azure、Google、Facebook 等身份验证提供程序。
我使用了SaasKit多租户程序集,在这里我尝试了app.usepertenant()中间件。但是UseGoogleAuthentication()方法已经过时了,所以我无法使用这个 usepertenant 中间件实现多租户身份验证。
当前代码,
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddMicrosoftAccount(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
Run Code Online (Sandbox Code Playgroud)
预期的代码如下,
var authentication = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
if (tenant.hasMicrosoft)
{
authentication.AddMicrosoftAccount(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
}
if (tenant.hasGoogle)
{
authentication.AddGoogle(option =>
{
option.ClientId = "clientid";
option.ClientSecret = "clientsecret";
option.SaveTokens = true;
});
}
authentication.AddCookie( options =>
{
options.SlidingExpiration = true; …Run Code Online (Sandbox Code Playgroud) authentication multi-tenant asp.net-core identityserver4 saaskit
我有一个简单的 Web api 项目,如下所示:
[Authorize]
[Route("Get")]
public ActionResult<string> SayHello()
{
return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试用邮递员测试它。按照此处的步骤操作:https://kevinchalet.com/2016/07/13/creating-your-own-openid-connect-server-with-asos-testing-your-authorization-server-with-postman/
1) 发送以下请求并按预期接收令牌:
2) 尝试使用授权令牌发送另一个请求,如下所示:
为什么我会收到 401(未经授权)错误?WWW-Authenticate 响应标头显示:Bearer error="invalid_token", error_description="The Issuer is invalid"。我正在使用.Net Core 3.1。我已经把截图中的敏感信息注释掉了。
从 MVC 应用程序访问时,Web api 按预期工作。
这是启动代码:
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = identityUrl; //identityurl is a config item
options.RequireHttpsMetadata = false;
options.ApiName = apiName;
});
Run Code Online (Sandbox Code Playgroud) scopeOpenID Connect、JWT 或 OAuth 中是否有任何标准声明?
在IdentityServer 4 文档中,有一个“范围”,它是一个空格分隔的字符串。
{
"client_id": "mobile_app",
"sub": "123",
"scope": "read write delete"
}
Run Code Online (Sandbox Code Playgroud)
但从我的 IdentityServer 4 实例中,我在访问令牌中得到了一个“范围”声明,它是一个字符串数组。
{
// ...
"client_id": "mobile_app",
"sub": "123",
"scope": [ "openid", "profile", "email", "offline_access" ],
"amr": [ "pwd" ]
}
Run Code Online (Sandbox Code Playgroud)
在OpenID Connect Core 1.0规范中,我没有看到“范围”被列为声明。在RFC 7519 JSON Web Token (JWT) 规范中,我没有看到“范围”被列为声明。
根据文档,IdentityServer使用非对称密钥对来签署和验证JWT.可以AddTemporarySigningCredential()在每次启动时创建新RSA的配置中使用,也可以使用AddSigningCredential(..)RSA密钥或证书.
该文档提到临时版本对于开发情况很有用,但它并没有说明在生产环境中使用它的缺点是什么.
我有一个aspnetcore web api,其中客户端使用IdentityServer4进行身份验证.该系统目前在temporarysigningcredential工作正常,但我想知道使用其他变体是否有任何好处.
谢谢,
我已经按照文档页面中的Quickstart进行操作,并使用IdentityServer进行身份验证,从而实现三种服务(IdentityServer,一种Api服务,一种ASPNET MVC应用程序)的工作配置.
一切正常(登录,登录,授权等),直到access_token到期后1小时.此时,MVC应用程序开始(正确地)从API服务接收401(因为令牌已过期).那时,我知道我应该使用refresh_token来获取新的access_token.
我正在寻找一种自动刷新access_token的机制,并偶然发现:https://github.com/mderriey/TokenRenewal/blob/master/src/MvcClient/Startup.cs(来自这个答案).我尝试使用它,但它不起作用(TokenEndpointResponse即使身份验证成功,也为null).
我理解如何使用a refresh_token来获取新的access_token,但是在我拥有它之后,我将如何将其插回到cookie中以便将来的请求可以访问新的令牌?
我需要帮助来了解时钟偏差的工作原理。我们定义时钟偏差来处理两方之间的时间变化。但是,我的困惑是:
那么,为什么我们需要时钟偏差呢?谁能给我一个例子来说明它是如何工作的以及它在哪些情况下会导致问题或好处?
我正在尝试向 .NET Core 3 API 项目添加身份服务器身份验证。
我已经添加了这个代码
public void ConfigureServices(IServiceCollection services)
{
…
var identityBuilder = services.AddIdentityServer();
identityBuilder.AddApiAuthorization<ApplicationUser, DbContext>();
services
.AddAuthentication()
.AddIdentityServerJwt();
var fileName = Path.Combine("Certificates", "certificatefile.pfx");
var cert = new X509Certificate2(fileName, "veryDifficultPassword");
identityBuilder.AddSigningCredential(cert);
…
}
Run Code Online (Sandbox Code Playgroud)
和:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
…
app.UseAuthentication();
app.UseIdentityServer(); // <--- this line throws error.
app.UseAuthorization();
…
}
Run Code Online (Sandbox Code Playgroud)
文件/Certificates夹中有一个文件可以正确读取和加载- 我可以检查cert变量并且它看起来是正确的。
到目前为止,我所尝试的一切都以线路app.UseIdentityServer();炸毁而告终:
System.InvalidOperationException: '未指定密钥类型。'
有什么建议?
更新:包括堆栈跟踪
System.InvalidOperationException
HResult=0x80131509
Message=Key type not specified.
Source=Microsoft.AspNetCore.ApiAuthorization.IdentityServer
StackTrace:
at Microsoft.AspNetCore.ApiAuthorization.IdentityServer.ConfigureSigningCredentials.LoadKey()
at …Run Code Online (Sandbox Code Playgroud) .net-core asp.net-core identityserver4 asp.net-core-identity asp.net-core-3.0
identityserver4 ×10
asp.net-core ×5
c# ×3
.net-core ×2
.net ×1
access-token ×1
json.net ×1
jwt ×1
multi-tenant ×1
oauth ×1
oauth-2.0 ×1
openid ×1
postman ×1
profiler ×1
saaskit ×1