【问题标题】:Access denied for user ''@'(DevMachine)' (using password: NO) MVC5 Identity MySQL用户''@'(DevMachine)'的访问被拒绝(使用密码:NO)MVC5 Identity MySQL
【发布时间】:2019-01-30 15:31:05
【问题描述】:

我正在开发一个 MVC 5 应用程序,它通过 Facebook 登录对用户进行身份验证。登录功能使用 SQL Server 工作。我正在将数据集合迁移到本地 mySQL 数据库。

我一直按照这两页上的说明进行操作: Implementing a custom MySQL Identity Storage Provider EF6 Support

登录部分正在工作,但我在视图中遇到上述错误:

@if (Request.IsAuthenticated && User.IsInRole(MyConstants.Defaults.Roles.Admin))

下面是我的构造函数:

public class ApplicationDbContext : MySQLDatabase
{
    public ApplicationDbContext(string connectionName)
        : base("DefaultConnection")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext("DefaultConnection");
    }
}

我的默认连接字符串包括用户名和密码。还有什么地方需要我提供参考吗?

31/08 更新

下面是错误的堆栈跟踪。除了调用 System.Web.Security.RolePrincipal 之外,执行直接跳转到 mysql Provider。我认为代码绕过了我设置的自定义身份提供程序 - 如果我在代码中明确调用它,它会起作用吗?

[MySqlException (0x80004005): Access denied for user ''@'DevMachine' (using password: NO)]
MySql.Data.MySqlClient.MySqlStream.ReadPacket() +309
MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket() +39

[MySqlException (0x80004005): Authentication to host '' for user '' using    method 
'mysql_native_password' failed with message: Access denied for user
''@'DevMachine' (using password: NO)]
MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.
AuthenticationFailed(Exception ex) +179
MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket() +64
MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate (Boolean reset) +381
MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset) +71
MySql.Data.MySqlClient.NativeDriver.Open() +831
MySql.Data.MySqlClient.Driver.Open() +22
MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings) +239
MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() +11
MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() +288
MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() +93
MySql.Data.MySqlClient.MySqlPool.GetConnection() +65
MySql.Data.MySqlClient.MySqlConnection.Open() +641
MySql.Web.Security.MySQLRoleProvider.GetRolesForUser(String username) +63
System.Web.Security.RolePrincipal.IsInRole(String role) +199
ASP._Page_Views_Shared__Menu_cshtml.Execute() in C:\Code\Studio-NI\Studio-NI\Views\Shared\_Menu.cshtml:23

【问题讨论】:

  • 错误信息表示服务器设置问题,而不是您的代码。尝试运行 mysqlworkbench 以确认 mysql 服务器上的用户权限。
  • 是的,看看错误信息...看起来你根本没有提供用户!
  • 我将 mySQL 中的用户更改为 DBA 并收到相同的错误消息。我有一个创建用户函数,它在启动时运行并传递默认连接字符串,并且运行良好。
  • @hackerman - 是的,这就是问题所在。对 asp.net 身份的调用正在使用 Microsoft 和 Oracle 内部代码来检查数据库 - 它没有使用我的连接字符串。我的 ApplicationDbContext 构造函数应该捕获所有调用 - 所以我想知道我是否错过了一些管道。

标签: mysql asp.net-mvc-5 asp.net-identity


【解决方案1】:

我在 Nuget 中使用预构建的 MySQL 身份提供程序尝试了另一个 mySQL 身份示例 - 在视图中添加 User.IsInRole 子句并没有炸毁应用程序,但无论角色如何,答案总是返回错误用户是其中的成员。

在与此问题相关的另一个 SO 问题中,提出了以下解决方案: https://stackoverflow.com/a/31325939/530762

在 Web.Config 中,我已经删除了 FormsAuthenticationManager,但添加这一行就可以了:

<modules>
    <remove name="RoleManager" />
</modules>

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 2016-01-28
    • 2022-12-29
    • 2019-12-29
    • 2020-03-12
    • 2017-07-25
    • 2016-07-11
    • 2021-10-19
    相关资源
    最近更新 更多