首页 > 解决方案 > 无法打开登录请求的数据库“Auth”。登录失败。用户“ServerName”登录失败

问题描述

我正在.net Core 2.1 中开发一个应用程序,我在其中应用 aspnet 身份用户的身份验证。数据库在服务器上,而我在 .net 核心中的程序在另一台带有 IIS 的服务器上。我的程序在本地正常工作,但是当我将它部署到我的服务器并尝试访问我的一种方法来执行登录时,我收到以下错误:

无法打开登录请求的数据库“Auth”。登录失败。用户“[NAME-OF-SERVER-WITH-IIS]”登录失败。

奇怪的是,在我的连接字符串中,我指定我要连接的用户是“sa”。

Startup.cs - ConfigureServices 方法

public void ConfigureServices(IServiceCollection services)
    {
        String con = "Data Source=<Servername>;Initial Catalog=Auth;Persist Security Info=True;User ID=sa;Password=<Password>;Trusted_Connection=True;MultipleActiveResultSets=true";
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(con,
          b => b.MigrationsAssembly("Auth")));
    ...
    }

AuthController.cs - 登录方法

[HttpPost("Login")]
    public async Task<IActionResult> Login([FromBody]CredentialsViewModel credentials)
    {
        try
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var identity = await GetClaimsIdentity(credentials.UserName, credentials.Password);

            if (identity == null)
                return BadRequest(Errors.AddErrorToModelState("login_failure", "Invalid username or password.", ModelState));

            var jwt = await Tokens.GenerateJwt(identity, jwtFactory, credentials.UserName, jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented });
            return new OkObjectResult(jwt);
        }
        catch (Exception e)
        {
            return new OkObjectResult(e.Message);
        }
    }

标签: sql-serveriis.net-coreasp.net-identity

解决方案


推荐阅读