asp.net-mvc - aspNet Core - 身份 - applicationUser,不是自动生成的吗?
问题描述
我想在使用 Microsoft 身份框架的网站中创建 Linkedin 登录。但我不知道如何使用 ApplicationUser,它说它丢失了,我不想生成一个空类。不知道该怎么办。Ms 身份框架应该自己创建一个吗?
我有(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
}
解决方案
如果您不需要自定义 Identity 框架中使用的 User 模型,那么只需使用IdentityUser
该类即可。向 User 模型添加自定义属性时,通常会使用该类ApplicationUser
(或您想调用的任何名称)。
改变这个services.AddIdentity<ApplicationUser, IdentityRole>()
:services.AddIdentity<IdentityUser, IdentityRole>()
更多信息可以在这里找到自定义身份模型
推荐阅读
- matlab - 通过在 Matlab 中调用终端命令处理显示的文本信息
- android - 为什么 AOSP 7.1.2 上出现 getpidcon() 错误?
- javascript - 如何在 NodeJS Web 应用程序中存储和访问大文件
- node.js - Graphql 未按预期解决
- javascript - 如何将道具传递给导出的函数?
- django - Django google app engine 没有这样的文件或目录
- java - 带有数据库的 ExpandableListView 中的 GridView
- android - Android:如何以编程方式捕获应用程序的网络流量?
- javascript - 为什么 JavaScript 中的 Call() 方法没有被压入堆栈?
- powershell - how to stop listing directories in powershell after you've entered dir -r?