我正在尝试在Web.Mvc
项目中大张旗鼓。
按照找到的步骤here
首先,我复制粘贴在Web.Host
项目Startup.cs
文件中找到的代码
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
//Swagger - Enable this line and the related lines in Configure method to enable swagger UI
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info { Title = "MySite API", Version = "v1" });
options.DocInclusionPredicate((docName, description) => true);
//Note: This is just for showing Authorize button on the UI.
//Authorize button's behaviour is handled in wwwroot/swagger/ui/index.html
options.AddSecurityDefinition("Bearer", new BasicAuthScheme());
});
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger();
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "MySite API V1");
options.IndexStream = () => Assembly.GetExecutingAssembly().GetManifestResourceStream("MySite.Web.wwwroot.swagger.ui.index.html");
}); //URL: /swagger
...
}
然后我将wwwroot\swagger
从Web.Host
复制到Web.Mvc
现在,当我导航到mysite.com/swagger
时,页面就会加载。在我尝试进行api调用之前,所有似乎都可以正常工作。
当我运行Web.Host
并在没有承载令牌的情况下拨打电话时,得到以下响应
{
"result": null,
"targetUrl": null,
"success": false,
"error": {
"code": 0,
"message": "Current user did not login to the application!",
"details": null,
"validationErrors": null
},
"unAuthorizedRequest": true,
"__abp": true
}
并在Web.Mvc
上进行相同的呼叫,我得到以下响应:
{
"result": {
"totalCount": 0,
"items": []
},
"targetUrl": null,
"success": true,
"error": null,
"unAuthorizedRequest": false,
"__abp": true
}
我希望未授权的请求将返回相同的响应,但不会。
我遇到的另一个问题是Web.Host
,我可以毫无问题地登录,但是在Web.Mvc
上我不能。登录返回错误代码400,并且我的TokenAuthController.Authenticate
上的断点从未命中(通过Web.Host
登录时,断点有效)。
我认为文档中可能缺少一些步骤,因为当我使用/api/TokenAuth/Authenticate
调用Web.Mvc
且未授权的请求并不表明它们未被授权时,我无法获得令牌