无法在web.mvc项目中实现摇摇欲坠

时间:2018-10-17 19:12:07

标签: c# asp.net-core aspnetboilerplate

我正在尝试在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\swaggerWeb.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且未授权的请求并不表明它们未被授权时,我无法获得令牌

0 个答案:

没有答案