D01 Using MySql as a Storage for IIS (Wpf, Xamarin) - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki

Please read the following article: https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider

Download and install Mysql in your local or virtual environment

picture

  • make sure to install "MySQL Workbench"

Create new user with "root"-privilege

  • run "MySQL Workbench"
  • login as root
  • execute the following script

picture

CREATE USER 'rt'@'%' IDENTIFIED BY '*********';
GRANT ALL PRIVILEGES ON *.* TO 'rt'@'%' WITH GRANT OPTION;
  • where
    • "rt" is a name (you can use any name you like)
    • "*********" - is a password (type the passwrd you like)
    • "%" - tells mysql that you can login not only from "localhost"-machine!!!!!

install Connector/NET on the developer machine (or on a machine where IIS is installed)

picture

install MySql.Data.EntityFramework

  • with VS2019 open Dm04WebApp and install MySql.Data.EntityFramework nuGet-package
    • choose the correct version with "Connector/NET"

picture

Modify Web.config (and App.config)

picture

  • App.config (We used small console app. So here is config file for it)

picture

Create AspNet Identity tables on the MySql server

  • run "MySQL Workbench"
  • login as root
  • execute the following script

picture

USE AspNetDbWebApplicationMobileSecurity;

CREATE TABLE `aspnetroles` (
  `Id` varchar(128) NOT NULL,
  `Name` varchar(256) NOT NULL,
  PRIMARY KEY (`Id`)
);

CREATE TABLE `aspnetusers` (
  `Id` varchar(128) NOT NULL,
  `Email` varchar(256) DEFAULT NULL,
  `EmailConfirmed` tinyint(1) NOT NULL,
  `PasswordHash` longtext,
  `SecurityStamp` longtext,
  `PhoneNumber` longtext,
  `PhoneNumberConfirmed` tinyint(1) NOT NULL,
  `TwoFactorEnabled` tinyint(1) NOT NULL,
  `LockoutEndDateUtc` datetime DEFAULT NULL,
  `LockoutEnabled` tinyint(1) NOT NULL,
  `AccessFailedCount` int(11) NOT NULL,
  `UserName` varchar(256) NOT NULL,
  PRIMARY KEY (`Id`)
  UNIQUE KEY `UNIQ_AspNetUsersUserName` (`UserName`)
);

CREATE TABLE `aspnetuserclaims` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `UserId` varchar(128) NOT NULL,
  `ClaimType` longtext,
  `ClaimValue` longtext,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `Id` (`Id`),
  KEY `UserId` (`UserId`),
  CONSTRAINT `ApplicationUser_Claims` FOREIGN KEY (`UserId`) REFERENCES `aspnetusers` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
);

CREATE TABLE `aspnetuserlogins` (
  `LoginProvider` varchar(128) NOT NULL,
  `ProviderKey` varchar(128) NOT NULL,
  `UserId` varchar(128) NOT NULL,
  PRIMARY KEY (`LoginProvider`,`ProviderKey`,`UserId`),
  KEY `ApplicationUser_Logins` (`UserId`),
  CONSTRAINT `ApplicationUser_Logins` FOREIGN KEY (`UserId`) REFERENCES `aspnetusers` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
);

CREATE TABLE `aspnetuserroles` (
  `UserId` varchar(128) NOT NULL,
  `RoleId` varchar(128) NOT NULL,
  PRIMARY KEY (`UserId`,`RoleId`),
  KEY `IdentityRole_Users` (`RoleId`),
  CONSTRAINT `ApplicationUser_Roles` FOREIGN KEY (`UserId`) REFERENCES `aspnetusers` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `IdentityRole_Users` FOREIGN KEY (`RoleId`) REFERENCES `aspnetroles` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ;

With console app create Databases and populate tables with data

  • we do not need to change the code!!!
  • just run our console app and create databases

picture

  • just run our console app and populate tables with data

picture

Start Dm04WebApp, run Uwp-app(or Android app) and register some user

  • Register new user

picture

  • here is a result:

picture

  • getting data:

picture