IdentityServer3 .Net Web API。收到错误 - 此请求的授权已被拒绝
IdentityServer3 .Net Web API. Getting error - Authorization has been denied for this request
我正在尝试使用 identityserver3 为我的 .net webapi 设置身份验证。
这是我在身份验证服务器项目Owin.Startup中的代码
public class Startup
{
public void Configuration(IAppBuilder app)
{
// hardcoded list of clients, scopes and users
var factory = new IdentityServerServiceFactory()
.UseInMemoryClients(clients)
.UseInMemoryScopes(scopes)
.UseInMemoryUsers(users);
app.UseIdentityServer(new IdentityServerOptions
{
SigningCertificate = new X509Certificate2($@"{AppDomain.CurrentDomain.BaseDirectory}\bin\my_selfsigned_cert.pfx", ConfigurationManager.AppSettings["certificatekey"]),
RequireSsl = false,
Factory = factory
});
}
下面是我网站中的代码api owin startup
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:45230"
});
app.UseWebApi(GlobalConfiguration.Configuration);
}
}
当我尝试登录身份服务器登录页面时,我的授权服务器似乎可以正常工作。我还可以通过发布到 /connect/token
来检索授权令牌
但是,当我使用收到的不记名令牌调用下面的 webapi 方法时,它总是失败并出现错误“{"Message": "Authorization has been denied for this request."}
Api -
[HttpGet]
[Authorize]
public IEnumerable<Customer> Get()
{
var customerRepository = new CustomerRepository();
return customerRepository.GetCustomers();
}
有人可以建议我缺少什么吗?
Microsoft.Owin.Host.SystemWeb
按照 中的建议将此 nuget 安装到我的网络 api 项目中,它开始工作了!!
我正在尝试使用 identityserver3 为我的 .net webapi 设置身份验证。
这是我在身份验证服务器项目Owin.Startup中的代码
public class Startup
{
public void Configuration(IAppBuilder app)
{
// hardcoded list of clients, scopes and users
var factory = new IdentityServerServiceFactory()
.UseInMemoryClients(clients)
.UseInMemoryScopes(scopes)
.UseInMemoryUsers(users);
app.UseIdentityServer(new IdentityServerOptions
{
SigningCertificate = new X509Certificate2($@"{AppDomain.CurrentDomain.BaseDirectory}\bin\my_selfsigned_cert.pfx", ConfigurationManager.AppSettings["certificatekey"]),
RequireSsl = false,
Factory = factory
});
}
下面是我网站中的代码api owin startup
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:45230"
});
app.UseWebApi(GlobalConfiguration.Configuration);
}
}
当我尝试登录身份服务器登录页面时,我的授权服务器似乎可以正常工作。我还可以通过发布到 /connect/token
来检索授权令牌
但是,当我使用收到的不记名令牌调用下面的 webapi 方法时,它总是失败并出现错误“{"Message": "Authorization has been denied for this request."}
Api -
[HttpGet]
[Authorize]
public IEnumerable<Customer> Get()
{
var customerRepository = new CustomerRepository();
return customerRepository.GetCustomers();
}
有人可以建议我缺少什么吗?
Microsoft.Owin.Host.SystemWeb
按照