Ciao,
ho sviluppato un sito con autenticazione usando web form e Identity framework.
Ho un problema con l'autenticazione, a volte, inserisco username e password corretti, ma l'autenticazione effettuata viene persa.
Se riciclo il pool in IIS, il problema si risolve, l'autenticazione ha successo e vengo rediretta alla homepage riservata (Default.aspx).
In dettaglio
Logon Method
protected void logon(object sender, EventArgs e)
{
ApplicationDbContext context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = userManager.Find(UserName.Text, Password.Text);
if (user != null)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
Response.Redirect("~/Default.aspx");
}
else
{
LabelError.Text = "Invalid username or password.";
}
}
Default.aspx page
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
HttpContext.Current.Response.Redirect("~/Account/Login.aspx");
}
In IdentityModels.cs file
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
Debug.Write("set database identity");
this.Database.CommandTimeout = 240;
}
}
Web.config file
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MyProject1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=MyProject1;Persist Security Info=True;User ID=MyProject1;Password=xxxx" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<caching>
<outputCache enableOutputCache="false" />
</caching>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Nel pool dell'applicazion in IIS 6.1, ho impostato
Load user profile: true
Identity: ApplicationPoolIdentity
Esempio
1 - Inserisco username e password (non appare messaggio "Invalid username or password");
2 - Vengo rediretta alla pagina di login
Se riciclo il pool in IIS, il problema si risolve
1 - Inserisco username e password
2 - Vengo rediretta alla pagina Default.aspx
Potete aiutarmi?
Grazie