[英]IdentityServer enable reference tokens
Current we are using JWT tokens to authenticate (which works) but I would like to use reference tokens. 当前,我们正在使用JWT令牌进行身份验证(有效),但我想使用参考令牌。 Currently our config is like this:
目前,我们的配置如下:
public static class Config
{
/// <summary>
/// Configures identity server
/// </summary>
public static void ConfigureIdentityServer(this IAppBuilder app, CormarConfig config)
{
// Create our options
var identityServerOptions = new IdentityServerOptions
{
SiteName = "Cormar API",
SigningCertificate = LoadCertificate(),
IssuerUri = "https://cormarapi-test.azurewebsites.net",
// Not needed
LoggingOptions = new LoggingOptions
{
EnableHttpLogging = true,
EnableWebApiDiagnostics = true,
EnableKatanaLogging = true,
WebApiDiagnosticsIsVerbose = true
},
// In membory crap just to get going
Factory = new IdentityServerServiceFactory().Configure(config),
// Disable when live
EnableWelcomePage = true
};
// Setup our auth path
app.Map("/identity", idsrvApp =>
{
idsrvApp.UseIdentityServer(identityServerOptions);
});
}
/// <summary>
/// Configures the identity server to use token authentication
/// </summary>
public static void ConfigureIdentityServerTokenAuthentication(this IAppBuilder app, HttpConfiguration config)
{
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "https://cormarapi-test.azurewebsites.net/identity",
DelayLoadMetadata = true,
ValidationMode = ValidationMode.Local,
RequiredScopes = new[] { "api" },
ClientId = "api",
ClientSecret = "not_my_secret"
});
AntiForgeryConfig.UniqueClaimTypeIdentifier = IdentityServer3.Core.Constants.ClaimTypes.Subject;
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
}
/// <summary>
/// Loads the certificate
/// </summary>
/// <returns></returns>
private static X509Certificate2 LoadCertificate()
{
var certPath = $"{ AppDomain.CurrentDomain.BaseDirectory }App_Data\\idsrv3test.pfx";
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, "3A1AFB6E1DC5C3F341E63651542C740DA4148866", false);
certStore.Close();
// If we are on azure, get the actual self signed certificate, otherwise return the test one
return certCollection.Count > 0 ? certCollection[0] : new X509Certificate2(certPath, "idsrv3test");
}
/// <summary>
/// Configure the identity service factory with custom services
/// </summary>
/// <returns></returns>
private static IdentityServerServiceFactory Configure(this IdentityServerServiceFactory factory, CormarConfig config)
{
var serviceOptions = new EntityFrameworkServiceOptions { ConnectionString = config.SqlConnectionString };
factory.RegisterOperationalServices(serviceOptions);
factory.RegisterConfigurationServices(serviceOptions);
factory.CorsPolicyService = new Registration<ICorsPolicyService>(new DefaultCorsPolicyService { AllowAll = true }); // Allow all domains to access authentication
factory.Register(new Registration<DbContext>(dr => dr.ResolveFromAutofacOwinLifetimeScope<DbContext>()));
factory.UserService = new Registration<IUserService>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IUserService>());
factory.ClientStore = new Registration<IClientStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IClientStore>());
factory.ScopeStore = new Registration<IScopeStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IScopeStore>());
return factory;
}
}
As you can see, I have added the ClientId and ClientSecret to IdentityServerBearerTokenAuthenticationOptions . 如您所见,我已将ClientId和ClientSecret添加到IdentityServerBearerTokenAuthenticationOptions中 。 If I set my client's AccessTokenType to reference and try to get a reference token, it works and I get a response like this:
如果我将客户端的AccessTokenType设置为引用并尝试获取引用令牌,则它可以正常工作,并且会收到如下响应:
"access_token": "631783604e9c35e6b401605fe4809075",
"expires_in": 3600,
"token_type": "Bearer"
But if I then try to access an resource on my server, I get a 401 unauthorized error. 但是,如果我随后尝试访问服务器上的资源,则会收到401未经授权的错误。 If I swap back to a JWT AccessTokenType I can authenticate and then access my resource with no issues.
如果我交换回JWT AccessTokenType ,则可以进行身份验证,然后毫无问题地访问我的资源。
As a note, I had set the ClientSecret and the ScopeSecret to the same value, so I would expect it to work. 注意,我已经将ClientSecret和ScopeSecret设置为相同的值,因此我希望它可以工作。
Am I forgetting to do something? 我忘了做某事吗?
You cannot locally verify a token when using the reference token type. 使用参考令牌类型时,您无法在本地验证令牌。 Since it's unstructured data, with no digitally verifiable signature, your API needs to check the token with IdentityServer.
由于它是非结构化数据,没有数字可验证的签名,因此您的API需要使用IdentityServer检查令牌。
To do this, change your ValidationMode
to ValidationMode.ValidationEndpoint
or ValidationMode.Both
为此,请将您的
ValidationMode
更改为ValidationMode.ValidationEndpoint
或ValidationMode.Both
From the docs: 从文档:
ValidationMode can be either set to Local (JWTs only), ValidationEndpoint (JWTs and reference tokens using the validation endpoint - and Both for JWTs locally and reference tokens using the validation endpoint (defaults to Both). - https://identityserver.github.io/Documentation/docsv2/consuming/options.html
可以将ValidationMode设置为Local(仅JWT),ValidationEndpoint(使用验证端点的JWT和参考令牌),以及对于本地JWT和使用验证端点的参考令牌(默认为Both)。- https://identityserver.github IO /文档/ docsv2 /消耗/ options.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.