Api 为什么[Route]在identity server 3中用于授权
我不熟悉开放ID、Identity Server和构建API, 我已经设置了Identity Server 3和API以及客户端,我的服务器将向客户端提供访问令牌,在调用API时可以使用该令牌 服务器中的启动是无效的Api 为什么[Route]在identity server 3中用于授权,api,oauth-2.0,identityserver3,Api,Oauth 2.0,Identityserver3,我不熟悉开放ID、Identity Server和构建API, 我已经设置了Identity Server 3和API以及客户端,我的服务器将向客户端提供访问令牌,在调用API时可以使用该令牌 服务器中的启动是无效的 public void Configuration (IAppBuilder app) { var options = new IdentityServerOptions { Facto
public void Configuration (IAppBuilder app)
{
var options = new IdentityServerOptions
{
Factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get())
.UseInMemoryUsers(Users.Get()),
RequireSsl = false
};
app.UseIdentityServer(options);
}
我的API启动了
public void Configuration(IAppBuilder app)
{
//accept access token from indentityserver and require a scope of api1
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:62172/",
ValidationMode = ValidationMode.ValidationEndpoint,
RequiredScopes = new[] { "api1" }
});
//config web api
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
// require authentication for all controllers
config.Filters.Add(new AuthorizeAttribute());
app.UseWebApi(config);
}
我的问题是为什么我使用
[Route("api/Search")]
它的工作原理类似于使用[授权]
[Route("api/Search")]
public async Task<IHttpActionResult> Companies(SearchRequest searchRequest)
{
}
[路由(“api/搜索”)]
公共异步任务公司(SearchRequest SearchRequest)
{
}
为什么上面的blow代码的工作方式是:
[Authorize]
public async Task<IHttpActionResult> Companies(SearchRequest searchRequest)
{}
[授权]
公共异步任务公司(SearchRequest SearchRequest)
{}
更多信息 在我的控制器中,这是我正在调用的方法,我试图强制用户进行授权
public async Task<IHttpActionResult> Companies(SearchRequest searchRequest)
{
var caller = User as ClaimsPrincipal;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
HttpResponseMessage response = new HttpResponseMessage();
Framework.BusinessLogicFactory factory = new Framework.BusinessLogicFactory();
BusinessLogic.Search search = factory.CreateSearch();
}
公共异步任务公司(SearchRequest)
{
var caller=用户作为ClaimsPrincipal;
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
HttpResponseMessage response=新的HttpResponseMessage();
Framework.BusinessLogicFactory工厂=新的Framework.BusinessLogicFactory();
BusinessLogic.Search=factory.CreateSearch();
}
但若我在控制器上并没有[Authorize]或[Route(“api/Sreach”)]属性,那个么对api的任何调用都将返回结果,
这就是我测试API的方式
string APiURL = "http://localhost:59791/api/Search";
var responses = GetClientToken();
var clinet = new HttpClient();
var value = new Dictionary<string, string>()
{
{ "CompanyNumber", " " },
{ "CompanyName", "test" },
{ "Address1", " " },
{ "Address2", " " },
{ "Address3", " " },
{ "PostCode", " " },
{ "CountryCode", " " },
};
var content = new FormUrlEncodedContent(value);
var response = await clinet.PostAsync(APiURL, content);
var t = response.StatusCode;
string apirl=”http://localhost:59791/api/Search";
var responses=GetClientToken();
var clinet=newhttpclient();
var值=新字典()
{
{“公司编号”,“公司编号”},
{“公司名称”,“测试”},
{“Address1”,“”},
{“地址2”,“”},
{“地址3”,“”},
{“邮政编码”,“”},
{“CountryCode”,“”},
};
var内容=新的FormUrlEncodedContent(值);
var response=wait clinet.PostAsync(apirl,content);
var t=响应状态码;
,因为您有config.Filters.Add(new AuthorizeAttribute())行
这将[Authorize]
应用于所有控制器。因此,导致应用授权的不是[Route]
,而是此筛选器。因此,如果是因为配置,它不应该在没有[Route]或[Authorize]的情况下工作吗??但是,当没有[route]或[authorize]时不会发生授权,并且当存在[route]或[authorize]时会发生授权。如果从控制器中删除所有属性,则过滤器仍会导致控制器需要授权。不,当控制器中没有属性时,授权不起作用,这就是为什么我不能找出授权工作的原因?在这种情况下,还有更多的故事。你最好在你的问题中加入你所说的“没有授权发生”(即,如果你得到数据)的意思,并从你的控制器中加入更多的代码。有人能告诉我为什么我的问题被扣分了吗??