【问题标题】:Identity project updated to Asp.Net Core 3 gives wwwroot folder更新到 Asp.Net Core 3 的身份项目提供 wwwroot 文件夹
【发布时间】:2020-01-12 03:17:06
【问题描述】:

我有一个 asp.net core 2.2.6 web api 项目。没有视图,没有脚本文件,没有 CSS。我刚刚将它升级到 asp.net core 3.1,当我使用发布版本发布时,我看到一个 wwwroot 文件夹,其中包含 css、js 和包含 css 和 js 文件的 lib 文件夹。

由于在我的解决方案中的任何地方都看不到它们,我该如何删除它们? 这是添加身份的代码:

更新 #1

services.AddDefaultIdentity<ApplicationUser>(options =>
{
    options.SignIn.RequireConfirmedEmail = true;
    options.Password.RequireDigit = true;
    options.Password.RequireLowercase = true;
    options.Password.RequireNonAlphanumeric = true;
    options.Password.RequireUppercase = true;
    options.Password.RequiredLength = 8;
}).AddRoles<ApplicationRole>()
.AddEntityFrameworkStores<MyDbContext>()
.AddUserManager<ApplicationUserManager>()
.AddDefaultTokenProviders();

// Set expiration time for confirmation links 
services.Configure<DataProtectionTokenProviderOptions>(options =>
{
    options.TokenLifespan = TimeSpan.FromMinutes(confirmationLinksExpirationInMinutes);
});

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); 
services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

})
.AddJwtBearer(cfg =>
{
    cfg.RequireHttpsMetadata = false;
    cfg.SaveToken = true;
    cfg.TokenValidationParameters = new TokenValidationParameters
    {
        ValidIssuer = Configuration["JwtBearerSettings:Issuer"],
        ValidAudience = Configuration["JwtBearerSettings:Issuer"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtBearerSettings:SecurityKey"])),
        ClockSkew = TimeSpan.Zero 
    };
});

更新 #2

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Autofac" Version="4.9.4" />
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="log4net" Version="2.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="3.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
  </ItemGroup>
  <ItemGroup>
    <Content Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Update="log4net.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

【问题讨论】:

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


    【解决方案1】:

    我认为自动生成的文件来自AddDefaultIdentity

    当使用AddDefaultUI(UIFramework.Bookstrap4) 时,ASP.NET Core 自动为所有身份视图使用视图(我认为其中有 28 个),但它们在解决方案中是隐藏的。您可以通过scaffolding identity 手动包含和修改它们。但是,当您删除这些视图时,它们不会被删除,而是会自动生成并再次隐藏。

    我猜创建的 CSS 和 JS 文件是为这些自动生成的视图自动生成的。

    您可以通过使用更改您的呼叫以添加身份以避免这种情况

    services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
    {
        options.SignIn.RequireConfirmedEmail = true;
        options.Password.RequireDigit = true;
        options.Password.RequireLowercase = true;
        options.Password.RequireNonAlphanumeric = true;
        options.Password.RequireUppercase = true;
        options.Password.RequiredLength = 8;
    })
    .AddEntityFrameworkStores<MyDbContext>()
    .AddUserManager<ApplicationUserManager>()
    .AddDefaultTokenProviders();
    

    根据您使用的是 .NET Core 2.2 还是 .NET Core 3.1,您可能需要添加额外的 NuGet 包。 (不确定我的记忆是否正确:Microsoft.AspNetCore.Identity.EntityFrameworkCore v2.2.0 和 v3.1.0 的行为不同。使用 v3.1.0 时可能需要添加 https://www.nuget.org/packages/Microsoft.AspNetCore.Identity/

    【讨论】:

    • 我的回答here可能对具体细节有所帮助。身份已切换为使用 3.0 的静态 Web 资产。
    • @citronas。我按照您的建议更改了代码,但输出仍然相同
    • @KirkLarkin 有没有办法删除这些文件?
    • 我根本不想要它们。我用视图等开发了自己的 spa 逻辑。所以没有任何目的。
    • 在这里找到解决方法github.com/dotnet/aspnetcore/issues/16862
    【解决方案2】:

    这是 Core 3.0+ 的新方法所有脚本、脚本库等都放在 wwwroot 文件夹中

    我曾经将所有这些脚本保存在 ~/Area/AreaName/scripts 中,但 core 3.0 不会从该目录中读取它们。

    这与安全性和阻止人们访问这些文件有关。

    【讨论】:

      猜你喜欢
      • 2016-07-17
      • 2023-04-08
      • 2019-06-10
      • 1970-01-01
      • 2021-01-17
      • 2017-11-12
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多