为了账号安全,请及时绑定邮箱和手机立即绑定

.NET Core Web API / Angular 应用程序中的 Windows 身份验证

.NET Core Web API / Angular 应用程序中的 Windows 身份验证

C#
慕桂英546537 2023-07-09 17:26:09
我正在使用 Visual Studio 2019 Community 构建一个 Intranet 应用程序,用于创建 .NET Core Web Api(使用 .NET Core 2.2),并使用 Visual Studio Code 来创建 Angular 前端(@angular/cdk 7.1.0、@angular/cli 7.0) .6,@角度/材料7.1.0)。由于它是一个 Intranet 应用程序,我想实现 Windows 身份验证,以便用户不必再次输入其凭据。我声明我已经尝试过这个和其他论坛但没有成功,我对这些技术不是很熟悉,所以我需要帮助来解决我遇到的一些问题,我可能不明白 CORS 是如何工作的。我也尝试从邮递员调用我的 Web API(默认设置=“授权:从父级继承身份验证”...),但结果相同。我已经从 NuGet 安装了 Microsoft.AspNetCore.Cors 并实现了以下代码。在 Web API 方面,我有这段代码。启动设置.json{  "$schema": "http://json.schemastore.org/launchsettings.json",  "iisSettings": {    "windowsAuthentication": true,    "anonymousAuthentication": false,    "iisExpress": {      "applicationUrl": "http://localhost:62148",      "sslPort": 0    }  },  "profiles": {    "IIS Express": {      "commandName": "IISExpress",      "launchBrowser": true,      "launchUrl": "api/values",      "environmentVariables": {        "ASPNETCORE_ENVIRONMENT": "Development"      }    },    "WebApiWinAuth": {      "commandName": "Project",      "launchBrowser": true,      "launchUrl": "api/values",      "applicationUrl": "http://localhost:5000",      "environmentVariables": {        "ASPNETCORE_ENVIRONMENT": "Development"      }    }  }}启动.cspublic void ConfigureServices(IServiceCollection services){                services.AddCors(c =>    {        c.AddPolicy("AllowOrigin",            options => options                    .AllowAnyOrigin()                    //.WithOrigins("http://localhost:4200")                    .AllowAnyMethod()                    .AllowAnyHeader()                );    });    services.AddMvc()        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)        .AddJsonOptions(options =>        {            var resolver = options.SerializerSettings.ContractResolver;            if (resolver != null)                (resolver as DefaultContractResolver).NamingStrategy = null;        });}
查看完整描述

2 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

anonymousAuthentication:true当我设置时它起作用launchSettings.json:


"iisSettings": {

"windowsAuthentication": true, 

"anonymousAuthentication": true, 

"iisExpress": {

  "applicationUrl": "http://localhost:62148",

  "sslPort": 0

}

从asp.net core中Windows身份验证和CORS的介绍,我将startup.cs更改如下:

  public void ConfigureServices(IServiceCollection services)

    {

        services.AddAuthentication(IISDefaults.AuthenticationScheme);//Add this line

        services.AddCors(c =>

        {

            c.AddPolicy("AllowOrigin",

                options => options

                        //.AllowAnyOrigin()

                        .WithOrigins("http://localhost:4200")

                        .AllowAnyMethod()

                        .AllowAnyHeader()

                        .AllowCredentials()//Add this line

                    );

        });


        services.AddMvc()

            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)

            .AddJsonOptions(options =>

            {

                var resolver = options.SerializerSettings.ContractResolver;

                if (resolver != null)

                    (resolver as DefaultContractResolver).NamingStrategy = null;

            });


    }


    public void Configure(IApplicationBuilder app, IHostingEnvironment env)

    {

        if (env.IsDevelopment())

        {

            app.UseDeveloperExceptionPage();

        }

        else

        {

            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.

            app.UseHsts();

        }


        app.UseCors("AllowOrigin");


        app.UseMvc();

    }

}


查看完整回答
反对 回复 2023-07-09
?
翻阅古今

TA贡献1780条经验 获得超5个赞

我是 .Net Core 的新手,在使用 Windows 身份验证时遇到类似的问题。我检查了很多帖子,但没有一个提供解决方案。通过 MVC 4 或 5 对 applicationhost.config 的更改解决了类似的问题。

我发现如果我更改端口,应用程序将以调试模式启动,并且窗口身份验证将正常工作。第二次启动该应用程序时,我会收到 401.1 或 401.2 错误。

从那以后我改用 Chrome 进行开发,它似乎工作得很好。这并不理想,因为我们的企业用户基础是 IE 或 Edge。


查看完整回答
反对 回复 2023-07-09
  • 2 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信