0

I'm developing an MVC 5 app which authenticates users with a Facebook Login. The Login functionality was working using SQL Server. I'm migrating the data collection to a local mySQL database.

I've been following the instructions on these two pages: Implementing a custom MySQL Identity Storage Provider EF6 Support

The login section is working but I'm encountering the above error in a view:

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

Below is my constructor:

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

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

My default connection string includes a username and password. Is there somewhere else I need to provide a reference to it?

Update 31/08

Below is the stack trace from the error. Apart from the call to System.Web.Security.RolePrincipal, the execution jumps straight into the mysql Provider. I think the code is bypassing the custom identity provider I set up - which works if I call it explicitly in the code?

[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
Spodgy
  • 302
  • 5
  • 13
  • the error message indicates a server setting problem, not your code. Try running mysqlworkbench to confirm the user privs on the mysql server. – john elemans Aug 29 '16 at 21:04
  • Yeah, just look at the error message...it looks like you are not providing an user at all! – Hackerman Aug 29 '16 at 21:08
  • I changed the user in mySQL to a DBA and am receiving the same error message. I have a create users function that runs at startup and passes the Default connection string and this runs fine. – Spodgy Aug 29 '16 at 21:19
  • @hackerman - yes, that is the issue. The call to asp.net identity is using Microsoft and Oracle internal code to check the database - and it's not using my connection string. My ApplicationDbContext constructor should be catching all calls - so I'm wondering if I've missed some plumbing. – Spodgy Aug 29 '16 at 21:38

1 Answers1

1

I tried another mySQL Identity example with a pre-built MySQL Identity provider in Nuget - Adding the User.IsInRole clause in the view didn't bomb the application, but the answer was always coming back false, regardless of the roles the user was a member of.

In another SO question relating to this issue the following solution was suggested: https://stackoverflow.com/a/31325939/530762

In the Web.Config I already had remove FormsAuthenticationManager but adding this line did the trick:

<modules>
    <remove name="RoleManager" />
</modules>
Community
  • 1
  • 1
Spodgy
  • 302
  • 5
  • 13