How to work with SimpleMembership in ASP.Net MVC in MySql? To deny and allow access to users

1

When you create a project in asp.net from visual studio, and choose the web application option, it generates some files that allow you to place some headings that are the following:

     [Authorize]
     [InitializeSimpleMembership]  

These allow me to validate if the user is logged in to allow access to the views.

My question is how can I do this myself, but when I create an empty / white project? I have seen some examples as described in the following link:

link

but it turns out that in my case it does not work, and most likely because the example is for SQLSERVER, and I'm using MySql.

When trying to prove as shown by the link example, I get the following error:

Unable to open the database "db_user" requested by the login. Login failed

this error gives in the login view that it is like this:

 public ActionResult Login()
    {
        if (!WebSecurity.Initialized)
        {
            WebSecurity.InitializeDatabaseConnection("MembershipDbContext", "usuarios", "password", "email", autoCreateTables: true);                
        }         
        return View();
    }

and in the webconfig it looks like this:

 <add name="MembershipDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=usuario_bd;Integrated Security=True" providerName="System.Data.SqlClient" />

but when I try to try it this way in the webconfig:

 <add name="MembershipDbContext" connectionString="SERVER=150.011.252.252;Initial Catalog=secure_login;Integrated Security=True" providerName="MySql.Data.MySqlClient" />

I get the following error:

  

Authentication to host '150.011.252.252' for user 'auth_windows' using method 'authentication_windows_client' failed with message: Access denied for user 'auth_windows'@'186-106-11-194.baf.movistar.cl' (using password: NO)

I would like to know what the solution to this would be. In any case, the objective is to be able to restrict users, not allowing access to views or pages if you are not logged in. Ideally, it would be to have roles, but it would be a separate issue.

    
asked by Danilo 16.02.2017 в 18:52
source

2 answers

0

I fixed the problem, in the file web.config I must place:

<sessionState mode="InProc" cookieless="false" regenerateExpiredSessionId="true" timeout="2880"/> 

The connectionString is over, and in Controller , where I log, I must place:

FormsAuthentication.SetAuthCookie(user, false)
    
answered by 17.02.2017 / 23:44
source
0

You must place this code in your menustrip:

Greetings.

    Private Sub CargarDatosToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CargarDatosToolStripMenuItem.Click
        Form2.MdiParent = Me
        Me.MenuStrip.Enabled = False
        Form2.Show()
        Form2.Focus()
    End Sub
    
answered by 17.02.2017 в 23:46