所以当我尝试运行我的项目时,我一直收到这个错误......该项目是由我的一位小组成员发送给我的。
“无法打开登录请求的数据库“身份”。登录失败。用户登录失败“
该项目本身是一个使用 MVC 的 Web 应用程序。我们有一个包含俱乐部/球员名单的数据库。我目前要做的只是运行它以查看我的小组已完成的工作,但它不断向我显示上述错误。我在网上广泛搜索无济于事,这就是我在这里发帖寻求帮助的原因。
以下是:
IdentitySeedData.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
namespace P0.Models
{
public static class IdentitySeedData
{
private const string adminUser = "Admin";
private const string adminPassword = "Secret123$";
public static async void EnsurePopulated(IApplicationBuilder app)
{
UserManager<IdentityUser> userManager = app.ApplicationServices
.GetRequiredService<UserManager<IdentityUser>>();
IdentityUser user = await userManager.FindByNameAsync(adminUser);
if (user == null)
{
user = new IdentityUser("Admin");
await userManager.CreateAsync(user);
}
}
}
}
AppIdentityContext.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace P0.Models
{
public class AppIdentityDbContext : IdentityDbContext<IdentityUser>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
: base(options) { }
}
}
程序.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace P0
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseDefaultServiceProvider(options =>
options.ValidateScopes = false)
.Build();
}
}
启动文件
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using P0.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
namespace P0
{
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.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration["Data:ConnectionStrings:SoccerClubs"]));
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(
Configuration["Data:ConnectionStrings:AppIdentityDbContext"]));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddTransient<IClubRepository, EFClubRepository>();
services.AddTransient<IPlayerRepository, EFPlayerRepository>();
services.AddMvc();
}
// 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.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
SeedData.EnsurePopulated(app);
IdentitySeedData.EnsurePopulated(app);
}
}
}
appsetting.json
{
"Data": {
"ConnectionStrings": {
"SoccerClubs": "Server=(localdb)\\MSSQLLocalDB;Database=SoccerClubs;Trusted_Connection=True;MultipleActiveResultSets=true",
"AppIdentityDbContext": "Server=(localdb)\\MSSQLLocalDB;Database=Identity;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
您的应用程序正在使用与 SQL Server 数据库的“可信连接”。所以它试图用计算机的当前用户登录。
您需要进入 SQL Server 对象资源管理器或 SQL Server Management Studio 并添加“DESKTOP\User”作为 SQL Server 的登录名和数据库中的用户。
以下是步骤
添加用户页面的屏幕截图
确保进入“用户映射”选项卡并勾选您要使用的数据库。如果这是开发,您可以勾选“db_owner”,以便您拥有所有权限。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句