【发布时间】: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