Asp.net web api .net 5 MVC 6 web api使用现有的identityDb进行身份验证
我正在努力使本机应用程序能够通过web api进行身份验证,该api使用从MVC6创建的现有identity db数据库。我理解这不是一种安全的方式来做事情。然而,在我弄明白如何让IdentityServer3与数据库一起工作之前,我想我会尝试一个简单的web api,它可以验证我在构建标准MVC6 web应用程序时创建的数据库。以下是我所做的: 从模板创建了asp.net 5 web api,并添加了以下内容: 设置: 我添加了appsettings.json:Asp.net web api .net 5 MVC 6 web api使用现有的identityDb进行身份验证,asp.net-web-api,asp.net-core,asp.net-core-mvc,Asp.net Web Api,Asp.net Core,Asp.net Core Mvc,我正在努力使本机应用程序能够通过web api进行身份验证,该api使用从MVC6创建的现有identity db数据库。我理解这不是一种安全的方式来做事情。然而,在我弄明白如何让IdentityServer3与数据库一起工作之前,我想我会尝试一个简单的web api,它可以验证我在构建标准MVC6 web应用程序时创建的数据库。以下是我所做的: 从模板创建了asp.net 5 web api,并添加了以下内容: 设置: 我添加了appsettings.json: "Data": { "
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-TestUsers-eaf0c85f-23e4-4603-97ce-b9f49ee1d167;Trusted_Connection=True;MultipleActiveResultSets=true"
}
},
启动:
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApiDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
DBContext:
public class ApiDbContext : IdentityDbContext<AppUser>
{
}
公共类ApiDbContext:IdentityDbContext
{
}
控制器:
private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager;
private readonly ILogger _logger;
...
public async Task<IEnumerable<string>> Post([FromBody]LoginModel model)
{
if (ModelState.IsValid) {
string user = model.userid;
string passwd = model.password;
var result = await _signInManager.PasswordSignInAsync(model.userid, model.password, false, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation(1, "User logged in.");
return new string[] { user };
}
else
{
return new string[] { "Failed" };
}
}
else
{
return new string[] { "Incorrect format received"};
}
}
private readonly UserManager\u UserManager;
专用只读签名管理器\u签名管理器;
专用只读ILogger\u记录器;
...
公共异步任务发布([FromBody]LoginModel模型)
{
如果(ModelState.IsValid){
字符串user=model.userid;
字符串passwd=model.password;
var result=await _signInManager.PasswordSignInAsync(model.userid,model.password,false,lockoutOnFailure:false);
if(result.successed)
{
_logger.LogInformation(1,“用户登录”);
返回新字符串[]{user};
}
其他的
{
返回新字符串[]{“失败”};
}
}
其他的
{
返回新字符串[]{“收到的格式不正确”};
}
}
但是,它会在_signInManager行中弹出错误:
System.NullReferenceException:对象引用未设置为实例
指一个物体
显然_signInManager为Null,因为我知道模型很好,因为我正在打印userid和密码,它们就在那里
我遗漏了什么,这样我就可以在web api中使用signInManager了?我又回到了另一次,看看web api和web应用之间有什么不同,因为web应用程序的身份验证工作正常。以下是我为使其正常工作而添加的内容: 控制器需要一个构造函数:
public AuthController(
SignInManager<AppUser> signInManager,
ILoggerFactory loggerFactory)
{
_signInManager = signInManager;
_logger = loggerFactory.CreateLogger<AuthController>();
}
将这些添加到上面允许我发布带有用户ID和密码的JSON。我又回到了另一个时间,看看web api和web应用之间有什么不同,因为web应用程序auth工作正常。以下是我为使其正常工作而添加的内容: 控制器需要一个构造函数:
public AuthController(
SignInManager<AppUser> signInManager,
ILoggerFactory loggerFactory)
{
_signInManager = signInManager;
_logger = loggerFactory.CreateLogger<AuthController>();
}
将这些添加到上面允许我发布带有用户ID和密码的JSON
services.AddIdentity<AppUser, IdentityRole>()
.AddEntityFrameworkStores<ApiDbContext>()
.AddDefaultTokenProviders();
app.UseIdentity();