I have a login control in my page. I have defined its provider and set up the configuration in web.config. However, it doesn't allow any user to enter. A user can only register a new account, then he can enter the website.

I tried these solutions,

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e949a0a5-030a-4932-ad92-fd45ee5ca36e/aspnet-login-control-not-working-correctly

Problem with Login control of ASP.NET

asp.net login control doesnt work

But, nothing worked.

This is the web.config:

<configuration>
  <connectionStrings>
    <add name="SportConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Sportflux.mdf;Integrated Security=True;Connect Timeout=30"
      providerName="System.Data.SqlClient" />

    <add name="LocalDBProviderConnectionString" connectionString="Data Source= (LocalDB)\MSSQLLocalDB;AttachDBFilename=|DataDirectory|\APNETDB.mdf; Integrated Security=True"
                                                                              providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <authorization>
      <allow roles="Admin" />
      <allow roles="Loged in user" />
    </authorization>
    <customErrors defaultRedirect="~/Error.aspx" />


    <authentication mode="Forms" >
     <forms loginUrl="Default.aspx" timeout="2880"/>
   </authentication>



    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2" />
   <roleManager enabled="true" defaultProvider="AspNaeSqlLocalDbProvider">
      <providers>

        <add connectionStringName="LocalDBProviderConnectionString" name="AspNaeSqlLocalDbProvider"
          type="System.Web.Security.SqlRoleProvider" />



      </providers>
    </roleManager>

    <membership defaultProvider="AspNaeSqlLocalDbProvider">
       <providers>
         <clear/>
         <add name="AspNaeSqlLocalDbProvider" connectionStringName="LocalDBProviderConnectionString"
             type="System.Web.Security.SqlMembershipProvider"/>
      </providers>
    </membership>


  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <system.net>
    <mailSettings>
      <smtp from="">
        <network host="localhost" password="" userName="" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Here is the login control and view code:

 <asp:LoginView ID="LoginView1" runat="server">
                    <AnonymousTemplate>
                      <asp:Login  ID="Login" runat="server" Orientation="Horizontal" PasswordRecoveryText="Forget My Password." 
                       PasswordRecoveryUrl="PasswordRecovery.aspx" DisplayRememberMe="true" FailureText="User Name or Passowrd is incorrect"
                       CreateUserText=" Create New Account." Height="75px" CreateUserIconUrl="SignUp.aspx" DestinationPageUrl="~/LoggedIn/Home.aspx" RememberMeSet="True" MembershipProvider="AspNaeSqlLocalDbProvider">
                         <LayoutTemplate>
                           <table  cellspacing="0" cellpadding="1" style="border-collapse: collapse;">
                             <tr>
                               <td>
                                 <table cellpadding="0">
                                    <tr>
                                       <td>
                                         <asp:Label runat="server" AssociatedControlID="UserName" ID="UserNameLabel">User Name:</asp:Label></td>
                                       <td>
                                         <asp:TextBox runat="server" ID="UserName"></asp:TextBox>
                                         <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ValidationGroup="ctl00$Login" ToolTip="User Name is required." ID="UserNameRequired">*</asp:RequiredFieldValidator>
                                       </td>
                                       <td>
                                         <asp:Label runat="server" AssociatedControlID="Password" ID="PasswordLabel">Password:</asp:Label>
                                         <asp:TextBox runat="server" TextMode="Password" ID="Password"></asp:TextBox>
                                         <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ValidationGroup="ctl00$Login" ToolTip="Password is required." ID="PasswordRequired">*</asp:RequiredFieldValidator>
                                       </td>
                                        <td>
                                         <asp:Button runat="server" CommandName="Login" Text="Log In" ValidationGroup="ctl00$Login" ID="LoginButton" Width="77px"></asp:Button>
                                        </td>
                                        </tr>
                                        <tr>
                                        <td></td>

                                        </td>
                                            <td>
                                         <asp:CheckBox runat="server" Text="Remember me." ID="RememberMe"></asp:CheckBox>
                                         <td><asp:CheckBox runat="server" OnCheckedChanged="forgeetenPass_CheckedChanged" Text="Forget password?" ID="forgeetenPass" AutoPostBack="True"></asp:CheckBox>
                                         &nbsp;<a class="a" href="SignUp.aspx">Create a new account ?</a></td>

                                        </td>
                                     </tr>

                                    </tr>

                                    <tr>
                                        <td></td><td colspan="3" style="color:red;">
                                    </td></tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </LayoutTemplate>
            </asp:Login>
          </AnonymousTemplate>
                    <LoggedInTemplate>
                        Welcome back, <asp:LoginName runat="server" /> | <asp:LoginStatus CssClass="a" runat="server" />
                    </LoggedInTemplate>
                </asp:LoginView>

The control doesn't allow anyone to login. I have tried the same code on another laptop, it allows logging in, but when trying to get the logged in username using this line User.Identity.Name.ToString() and store in a session, I find that the session is empty, I get a null reference error.

Can anyone please tell me what is the solution to this problem?

Thanks.

Related posts

Recent Viewed