VahidN / DNTIdentity

A highly customized sample of the ASP.NET Core Identity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Upgrading to .NET Core 2.0

SirwanAfifi opened this issue · comments

I followed your steps for upgrading my project to .NET Core 2.0, the process was straightforward, thank you. now when I want to login to my website an exception will be thrown, here's the stack trace for the exception.

As you can see, it's because this line of code (ApplicationClaimsPrincipalFactory.cs), I have no idea why but it seems that it can't create Claim Principle for logged in user:

var principal = await base.CreateAsync(user).ConfigureAwait(false);

Here are my csproj file for each project:
MyProject.Core:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <AssemblyName>MyProject.Core</AssemblyName>
    <PackageId>MyProject.Core</PackageId>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="../MyProject.Domain/MyProject.Domain.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="1.1.10" />
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
    <PackageReference Include="Automapper" Version="5.1.1" />
    <PackageReference Include="iTextSharp.LGPLv2.Core" Version="1.2.3" />
    <PackageReference Include="MailKit" Version="1.10.1" />
    <PackageReference Include="CsvHelper" Version="2.16.3" />
    <PackageReference Include="ImageSharp" Version="1.0.0-alpha9-00112" />
    <PackageReference Include="System.ValueTuple" Version="4.3.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore">
      <HintPath>..\..\..\..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.identity.entityframeworkcore\2.0.0\lib\netstandard2.0\Microsoft.AspNetCore.Identity.EntityFrameworkCore.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

MyProject.Domain:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <DebugType>portable</DebugType>
    <AssemblyName>MyProject.Domain</AssemblyName>
    <PackageId>MyProject.Domain</PackageId>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    <PackageReference Include="System.ComponentModel.Annotations" Version="4.4.0" />
    <PackageReference Include="System.ValueTuple" Version="4.3.1" />
  </ItemGroup>
  <ItemGroup>
    <Compile Update="Models\BaseModel.cs">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Compile>
  </ItemGroup>
</Project>

MyProject.Web:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <AssemblyName>MyProject.Web</AssemblyName>
    <OutputType>Exe</OutputType>
    <PackageId>MyProject.Web</PackageId>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="../MyProject.Domain/MyProject.Domain.csproj" />
    <ProjectReference Include="../MyProject.Core/MyProject.Core.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="Automapper" Version="5.1.1" />
    <PackageReference Include="iTextSharp.LGPLv2.Core" Version="1.2.3" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.1.4" />
    <PackageReference Include="System.ValueTuple" Version="4.4.0" />
  </ItemGroup>
  <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <Exec Command="npm install" />
    <Exec Command="bower install" />
  </Target>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>
  <ItemGroup>
    <Content Update="appsettings.json" CopyToPublishDirectory="Never" />
  </ItemGroup>
</Project>

Also here's my Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>();
    var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));
    services.Configure<JwtIssuerOptions>(options =>
    {
        options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)];
        options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)];
        options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256);
    });
    services.AddAuthentication().AddJwtBearer(cfg =>
    {
        cfg.TokenValidationParameters = new TokenValidationParameters()
        {
            ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],
            ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = _signingKey,
            ValidateLifetime = true
        };
    });
    services.AddMvc();
    services.AddScoped<ISecurityStampValidator, CustomSecurityStampValidator>();
    services.AddScoped<SecurityStampValidator<User>, CustomSecurityStampValidator>();
    services.AddScoped<SignInManager<User>, ApplicationSignInManager>();
    services.AddScoped<ApplicationUserManager, ApplicationUserManager>();
    services.AddScoped<UserManager<User>, ApplicationUserManager>();
    services.AddScoped<IUserClaimsPrincipalFactory<User>, ApplicationClaimsPrincipalFactory>();
    services.AddScoped<UserClaimsPrincipalFactory<User, IdentityRole>, ApplicationClaimsPrincipalFactory>();
    services.Configure<IdentityOptions>(options =>
    {
    });
    services.ConfigureApplicationCookie(identityOptionsCookies => {
        var provider = services.BuildServiceProvider();
        identityOptionsCookies.AccessDeniedPath = new PathString("/Account/Login");
    });
    services.AddIdentity<User, IdentityRole>(options =>
    {
        options.Password.RequireDigit = false;
        options.Password.RequireLowercase = false;
        options.Password.RequireUppercase = false;
        options.Password.RequireNonAlphanumeric = false;
        options.Password.RequiredLength = 3;

    }).AddEntityFrameworkStores<AppDbContext>()
        .AddDefaultTokenProviders();
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
    var serviceProvider = services.BuildServiceProvider();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseAuthentication();
    // other middleware: UseDeveloperExceptionPage, app.UseStaticFiles(); app.UseSession(); app.UseMvc....
}

Did I miss something?

SqlException: Invalid column name 'UserId'. means you have not updated your entities to add missing properties. Your table doesn't have the UserId field (That's why you have an SqlException). Also make sure you have the correct mappings too. After these changes you should apply the migrations again.

commented

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related problems.