Guest User

Untitled

a guest
Nov 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <connectionStrings>
  2. <add name="MySQLContext" connectionString="server=localhost;database=db;uid=root;password=xxxxxxxx" providerName="MySql.Data.MySqlClient" />
  3. </connectionStrings>
  4.  
  5. <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
  6. <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
  7. <providers>
  8. <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
  9. <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
  10. </providers></entityFramework>
  11.  
  12. public class UsersContext : DbContext
  13. {
  14. public UsersContext()
  15. : base()
  16. {
  17. }
  18.  
  19. // Constructor to use on a DbConnection that is already opened
  20. public UsersContext(DbConnection existingConnection, bool contextOwnsConnection)
  21. : base(existingConnection, contextOwnsConnection)
  22. {
  23.  
  24. }
  25.  
  26. public DbSet<LoginUsers> LoginUsers { get; set; }
  27. }
  28.  
  29. [Table("login_users")]
  30. public class LoginUsers
  31. {
  32. public int Id { get; set; }
  33. public string Username { get; set; }
  34. public string Password { get; set; }
  35. public string Name { get; set; }
  36. public DateTime Created_At { get; set; }
  37. public bool Active { get; set; }
  38. }
  39.  
  40. private static string connectionString = ConfigurationManager.ConnectionStrings["MySQLContext"].ConnectionString;
  41.  
  42. public static bool RegisterUser(string name, string username, string password, out string errorMessage)
  43. {
  44. string hashedPassword = SHA256Encrypt(password);
  45.  
  46. using (MySqlConnection connection = new MySqlConnection(connectionString))
  47. {
  48. connection.Open();
  49. MySqlTransaction transaction = connection.BeginTransaction();
  50.  
  51. try
  52. {
  53. // DbConnection that is already opened
  54. using (UsersContext context = new UsersContext(connection, false))
  55. {
  56. // Passing an existing transaction to the context
  57. context.Database.UseTransaction(transaction);
  58.  
  59. context.LoginUsers.Add(new LoginUsers()
  60. {
  61. Name = name,
  62. Username = username,
  63. Password = hashedPassword,
  64. Created_At = DateTime.UtcNow,
  65. Active = true
  66. });
  67. context.SaveChanges();
  68. }
  69.  
  70. errorMessage = "";
  71. transaction.Commit();
  72. return true;
  73. }
  74. catch (Exception ex)
  75. {
  76. transaction.Rollback();
  77. errorMessage = "Contain error";
  78. return false;
  79. }
  80. }
  81. }
  82.  
  83. context.LoginUsers.Add(new LoginUsers()
  84. {
  85. Name = name,
  86. Username = username,
  87. Password = hashedPassword,
  88. Created_At = DateTime.UtcNow,
  89. Active = true
  90. });
  91.  
  92. DEFINER = `admin`@`%`
  93.  
  94. connectionString="server=localhost;user id=myUserId;Password=*****;database=mydatabase; Allow User Variables=True"
Add Comment
Please, Sign In to add comment