我是IdentityServer和OpenId连接的新手。我正在使用.NET框架6以及IdentityServer3和OpenId连接。我已经完成了IdentityServer3文档概述部分中的三个演练--使用控制台客户端、MVC客户端和JavaScript客户端--并将这些工作解决方案作为模型。我现在正尝试使用一个简单的MVC客户端来构建我自己的身份验证服务,以便进行测试。这不管用。其不起作用的症状是,MVC页面(使用Home控制器中的Author权属性保护)报告HTTP 401.0 --未经授权而不将用户重定向到登录页面。我已经查看了Chrome开发工具中的网络流量,并看到了对/Home/关于返回HTTP 401的初始请求,而不是预期的HTTP 302重定向。
我的IdentityServer3配置如下所示:
public class Startup
{
public void Configuration(IAppBuilder app)
{
// TODO: Replace use of InMemoryUsers with custom Orvis UserService
app.UseIdentityServer(new IdentityServerOptions
{
SiteName = "MyClient Authentication Service",
SigningCertificate = LoadCertificate(),
Factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get())
.UseInMemoryUsers(Users.Get().ToList()),
RequireSsl = true
});
}
X509Certificate2 LoadCertificate()
{
// TODO: Get real signing certificate?
return new X509Certificate2(string.Format(@"{0}\bin\idsrv3test.pfx", AppDomain.CurrentDomain.BaseDirectory), "idsrv3test");
}
}
GetClients和GetScopes代码如下所示:
public static IEnumerable<Client> Get()
{
// TODO: Replace hard-coded implementation of Clients.Get() with a configuration-driven implementation
return new List<Client>
{
new Client
{
Enabled = true,
ClientId = "mvc-sample",
ClientName = "MVC Sample Client",
RequireConsent = false,
Flow = Flows.Implicit,
RedirectUris = new List<string> { "http:/localhost:37320" },
AllowedCorsOrigins = new List<string> { "http://localhost:37320/" },
AllowedScopes = new List<string> { "orvis-services", "orvis-shopper-service" }
}
};
}
public static IEnumerable<Scope> Get()
{
return new List<Scope>
{
new Scope
{
Name = "all-services",
DisplayName = "All Services",
Description = "Access to all microservices",
Type = ScopeType.Resource
},
new Scope
{
Name = "shopper-service",
DisplayName = "Shopper Service",
Description = "Access to the Shopper microservice",
Type = ScopeType.Resource
}
};
}
我的客户端配置如下:
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://localhost:44332",
ClientId = "mvc-sample",
RedirectUri = "http://localhost:37320",
ResponseType = "id_token",
SignInAsAuthenticationType = "Cookies"
});
}
因此,非常类似于IdentityServer3“入门: MVC身份验证和Web”。一个不同之处是,遍历过程中的客户端代码和服务器代码位于同一个项目中,我已经将它们分成了不同的项目。
由于dev tools网络选项卡显示了使用401.0响应(而不是预期的302响应)对/Home/About的请求,因此我怀疑cilent设置有问题。但是,除了浏览器开发工具之外,我不知道从哪里获取底层细节。
如有任何建议请见谅。
发布于 2017-03-23 15:55:24
问题是,我作为模型使用的IdentityServer3在一个项目中有IdentityServer和MVC客户端。当我分别创建它们时,我将Microsoft.Owin.Host.Systemweb和IdentityServer3包添加到IdentityServer项目中,将Microsoft.Owin.Security.Cookies和Microsoft.Owin.Security.OpenIdConnect包添加到MVC客户端项目中。事实证明,MVC客户端项目还需要Microsoft.Owin.Host.Systemweb包。没有它,Startup.Configuration()方法就不会被调用。
https://stackoverflow.com/questions/42977877
复制相似问题