据我所知,WebApi AuthorizeAttribute将搜索Bearer标头以从那里获取访问令牌。但像AuthHttp这样的游戏并没有发送这个标题。
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Type:application/json
Host:localhost:58928
Origin:http://localhost:4200
Pragma:no-cache
Referer:http://localhost:4200/catalogues
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.3
这就是它的配置方式
export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig({
tokenName: 'token',
noJwtError: true,
tokenGetter: (() => sessionStorage.getItem('token')),
globalHeaders: [{'Content-Type': 'application/json'}],
}), http, options);
}
....
providers: [
SearchService,
DataService,
StorageService,
LocalStorageService,
AuthService,
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions]
},
AuthGuard
],
然后是WebApi控制器
[ScopeAuthorize("read:userprofile")]
[HttpGet]
[ResponseType(typeof(CatalogueListDto))]
public async Task<IHttpActionResult> Get()
{
var result = await _catalogueService.GetCatalogues();
if (result == null) return BadRequest(ActionAnswer.Failed.CatalogueNotFound);
return Ok(result);
}
我做错了什么?