Search code examples
asp.net-mvcasp.net-identitylinkedin-api

aspNet Core - identity - applicationUser, not automatically generated?


I want to create Linkedin login into a website that uses Microsoft identity framework. But i don´t know how to use the ApplicationUser, it says its missing, and i don´t want to generate a empty class. Don´t know what to do with it. should Ms identity framework create one by its self?

I have (ms studio 2017 v15.9.31) .NET framework v4.8 aspnet core app 2.1.1 , aspnetCore razor design 2.1.2 , aspnet security oauth linkedin 2.0.0

using System.Threading.Tasks;
using Linkedin.Data;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Linkedin
{
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
       
        services.AddAuthentication().AddLinkedIn(options => {
            options.ClientId = Configuration["Authentication:LinkedIn:ClientId"];
            options.ClientSecret = Configuration["Authentication:LinkedIn:ClientSecret"];
            options.Events = new OAuthEvents()
            {
                OnRemoteFailure = loginFailureHandler => 
            {

                var authProperties = options.StateDataFormat.Unprotect(loginFailureHandler.Request.Query["state"]);
                loginFailureHandler.Response.Redirect("/Account/login");
                loginFailureHandler.HandleResponse();
                return Task.FromResult(0);
            }
            };
        });

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            //app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    } //configure
} // startup

}

Solution

  • If you don't need to customise the User model used in the Identity framework then just use the IdentityUser class. The ApplicationUser class (or whatever you want to call it) is usually used when adding custom properties to the User model.

    Change this: services.AddIdentity<ApplicationUser, IdentityRole>() to services.AddIdentity<IdentityUser, IdentityRole>()

    More info can be found here Customizing Identity Model