簡體   English   中英

.NET Core的自托管Web API出現問題

[英]Trouble with self-hosting web API with .NET Core

我無法在.NET Core中自托管我的Web API。 它在IIS上運行良好,但在嘗試自我托管時出現身份驗證錯誤。 我嘗試在其他解決方案中使用Cookie身份驗證,但徒勞無功。 我現在只有1條路線,但是在放置多個斷點之后,我注意到它甚至沒有到達我的controlelr的構造函數。 如果有人可以給我一個解決方案的提示,我將不勝感激:)

這是我的代碼片段。

啟動文件

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);
    }
}

這是我得到的錯誤

在此處輸入圖片說明

您需要身份驗證,但您的應用程序中沒有任何身份驗證中間件來處理它。 您需要使用UseCookieAuthentication或UseJwtBearerAuthentication之類的東西。 它不會在IIS中失敗,因為IIS為Windows身份驗證添加了中間件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM