【发布时间】:2017-10-19 21:19:50
【问题描述】:
我已经在本地设置了 IdentityServer3,一切正常。 我正在使用 JWT 对我的用户进行授权,并且能够成功访问我的 Web API 控制器(使用 authorize 属性)。
当我上传到 azure 时,虽然我可以获得访问令牌,但是当我尝试访问控制器时,我收到 401 错误。 我认为这与证书有关。 我的配置如下:
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://localhost:44313",
// 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://localhost:44313/identity",
ValidationMode = ValidationMode.ValidationEndpoint,
RequiredScopes = new[] { "api" }
});
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";
return 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;
}
}
我一直在阅读,看起来如果我使用参考令牌,我不需要使用证书来签署它们。所以我将我的客户端 AccessTokenType 更改为引用令牌并将秘密添加到 api 范围内,我能够在本地访问受保护的控制器,但是当我再次推送到天蓝色时,我仍然得到 401。
有谁知道我该如何解决这个问题?
【问题讨论】:
-
但是如果在天蓝色中运行,为什么你有本地主机和端口 44313?这个端口是否在 Azure 防火墙中打开? (站点配置)?
-
你可以随时发推文Dominick Baier
-
@rmjoia IssuerUri 可以随时更改。那只是测试时的本地主机,但这不会导致我遇到的问题
-
所以..没有导致问题的代码,我不知道出了什么问题:)无论如何..端口和东西适用..
-
issuerUri 不影响代码。这只是一个 issuerUri....
标签: c# azure identityserver3