2

Recently I've tried adding user accounts to my app (simple web app, had the /Account folder by default). It worked by default when using MSSQL but I needed MySQL. Followed some tutorials and changed to MySQL. All good so far, I can create (Register) users and the MySQL gets populated (only the aspnetusers table gets populated, not sure if the others should get populated). My problem is the LoggedInTemplate is not showing up (i do get an error IF I input the wrong credentials otherwise it just redirects me to the Default page showing me as not logged in). So basically shows up instead of LoggedInTemplate. I also tried connecting to MSSQL but I get the same result. I'm also kind of new to ASP.NET. Help?

Site.master -> LoginView:

                    <asp:LoginView runat="server">
                    <AnonymousTemplate>
                        <ul class="nav navbar-nav navbar-right">
                            <li><a runat="server" href="~/Account/Register">Register</a></li>
                            <li><a runat="server" href="~/Account/Login">Log in</a></li>
                        </ul>
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        <ul class="nav navbar-nav navbar-right">
                            <li><a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName()  %> !</a></li>
                            <li>
                                <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
                            </li>
                        </ul>
                    </LoggedInTemplate>
                </asp:LoginView>

Web.config -> relevant info:

    <connectionStrings>
    <add name="smarteraspmysql" providerName="MySql.Data.MySqlClient" connectionString="server=-;uid=-;persistsecurityinfo=True;database=-;password=-" />  </connectionStrings>
  <system.web>
    <customErrors mode="Off" />
    <authentication mode="Forms">
      <forms loginUrl="~/Login.aspx" defaultUrl="Default.aspx" cookieless="UseCookies" path="/"></forms>
    </authentication>

<!-------------------------------------------------------->    
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"/>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
    </DbProviderFactories>
  </system.data>
</configuration>

IdentityModels.cs:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext() : 
           base("smarteraspmysql")

        {

        }

    }

Login.aspx.cs (left unmodified):

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register";
            OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }
        }

        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                // Validate the user password
                var manager = new UserManager();
                ApplicationUser user = manager.Find(UserName.Text, Password.Text);
                if (user != null)
                {
                    IdentityHelper.SignIn(manager, user, RememberMe.Checked);
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    FailureText.Text = "Invalid username or password.";
                    ErrorMessage.Visible = true;
                }
            }
        }
Nick
  • 21
  • 3

0 Answers0