【问题标题】:Trouble with self-hosting web API with .NET Core使用 .NET Core 的自托管 Web API 出现问题
【发布时间】:2016-09-28 17:42:13
【问题描述】:

我在 .NET Core 中自托管我的 Web API 时遇到问题。它在 IIS 上运行良好,但在尝试自托管时出现身份验证错误。我尝试在其他解决方案中使用 cookie 身份验证,但徒劳无功。我现在只有 1 条路线,但是在放置多个断点后,我注意到它甚至没有到达我的控制器的构造函数。如果有人能给我一个解决方案的提示,我将不胜感激:)

这是我的代码的 sn-ps。

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseApplicationInsightsRequestTelemetry();

    app.UseApplicationInsightsExceptionTelemetry();

    app.UseMvc();
}

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddApplicationInsightsTelemetry(this.Configuration);

    // Add the database used
    services.AddDbContext<VaultContext>(options => options.UseSqlServer(this.Configuration.GetConnectionString("VaultDatabase")));

    // Add our repository type
    services.AddScoped<VaultRepository, VaultRepository>();
    services.AddScoped<UserResolverService, UserResolverService>();
    services.AddScoped<PersonalizationServiceClient, PersonalizationServiceClient>();
    services.AddScoped<PersistenceToDataModelConverter, PersistenceToDataModelConverter>();

    services.AddMvc(config =>
    {
        var policy = new AuthorizationPolicyBuilder()
                            .RequireAuthenticatedUser()
                            .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
    });
}

''services.AddMvc'' 中的配置 lamda 是我发现的唯一有效的方法,它迫使调用者提供他的 Windows 凭据,我稍后将根据他的配置文件分析和显示信息。如果这不是正确的方法,请告诉我。

还有我的控制器类

public VaultController(VaultRepository repository, UserResolverService currentuser, PersonalizationServiceClient personalizationService, PersistenceToDataModelConverter persistenceToDataModelConverter)
{
    this.repository = repository;
    this.currentuser = currentuser;
    this.personalizationService = personalizationService;
    this.persistenceToDataModelConverter = persistenceToDataModelConverter;
}

/// <summary>
/// The get profile.
/// </summary>
/// <returns>
/// The <see cref="IActionResult"/>.
/// </returns>
[HttpGet]
[Route("/Profile")]
[Produces(typeof(UserProfile))]
public IActionResult SearchProfile()
{
    try
    {
        if (!this.currentuser.IsAuthenticated)
        {
            throw new Exception("This service does not support anonymous calls.");
        }

        var profile = Task.Run(() => this.personalizationService.GetUserProfileAsync(this.currentuser.GetCurrentWindowsIdentityName)).Result;

        var userProfile = this.persistenceToDataModelConverter.Convert(profile);
        userProfile.UserAdLogin = this.currentuser.GetCurrentWindowsIdentityName;

        return this.Ok(userProfile);
    }
    catch (Exception ex)
    {
        return this.NotFound(ex);
    }
}

这是我得到的错误

【问题讨论】:

    标签: c# asp.net .net api asp.net-core


    【解决方案1】:

    您需要身份验证,但您的应用中没有任何身份验证中间件来处理它。您需要使用 UseCookieAuthentication 或 UseJwtBearerAuthentication 之类的东西。它不会在 IIS 中失败,因为 IIS 为 Windows auth 添加了中间件。

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 2021-10-04
      • 2017-02-05
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 2021-02-14
      • 2014-07-24
      • 2017-05-07
      相关资源
      最近更新 更多