aspNet Core - identity - applicationUser,不是自动生成的吗?
aspNet Core - identity - applicationUser, not automatically generated?
我想在使用 Microsoft 身份框架的网站上创建 Linkedin 登录。但我不知道如何使用 ApplicationUser,它说它丢失了,我不想生成一个空的 class。不知道该怎么办。 Ms identity framework要不要自己创建一个?
我有 (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
}
如果您不需要自定义身份框架中使用的用户模型,则只需使用 IdentityUser
class。 ApplicationUser
class(或者随便你怎么称呼它)通常在向用户模型添加自定义属性时使用。
更改此:services.AddIdentity<ApplicationUser, IdentityRole>()
到 services.AddIdentity<IdentityUser, IdentityRole>()
可以在此处找到更多信息Customizing Identity Model
我想在使用 Microsoft 身份框架的网站上创建 Linkedin 登录。但我不知道如何使用 ApplicationUser,它说它丢失了,我不想生成一个空的 class。不知道该怎么办。 Ms identity framework要不要自己创建一个?
我有 (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
}
如果您不需要自定义身份框架中使用的用户模型,则只需使用 IdentityUser
class。 ApplicationUser
class(或者随便你怎么称呼它)通常在向用户模型添加自定义属性时使用。
更改此:services.AddIdentity<ApplicationUser, IdentityRole>()
到 services.AddIdentity<IdentityUser, IdentityRole>()
可以在此处找到更多信息Customizing Identity Model