How to configure RedisSessionStateProvider to use Redis sentinel? - asp.net

I have a demo ASP.NET app with the session store configured to use a single Redis master node using the Microsoft.Web.Redis.RedisSessionStateProvider nuget (config below).
As a next step I would like to change my Redis store to be high availability by adding slave and sentinel nodes. Question (1): does the RedisSessionStateProvider support this, and (2) if so, how do I configure RedisSessionStateProvider to refer to sentinel nodes.
<sessionState mode="Custom" customProvider="RedisProvider" timeout="240" cookieless="UseCookies" cookieName="ASP.NET_SessionId" useHostingIdentity="true">
<providers>
<add name="RedisProvider"
type="Microsoft.Web.Redis.RedisSessionStateProvider, Microsoft.Web.RedisSessionStateProvider"
host="127.0.0.1"
port="6379"
accessKey=""
ssl="false" />
</providers>
</sessionState>

To answer your question quickly... remove host, port, accessKey, and ssl attributes from the provider config and add connectionString. Enter the correct connection string information for your case: ie - "192.168.1.10:6379,192.168.1.11:6379,192.168.1.12:6379,ssl=false,password=My_Super_Secret_Password". Since you are using a cluster configuration all node passwords have to be the same for this to work correctly. You also need to provide the applicationName attribute like applicationName="TestApp" so your web.config should look something like the following:
<sessionState mode="Custom" cookieName="_web.ss" customProvider="RedisSessionStateProvider" cookieless="false" timeout="10080">
<providers>
<clear />
<add name="RedisSessionStateProvider"
type="Microsoft.Web.Redis.RedisSessionStateProvider"
applicationName="TestApp"
connectionString="192.168.1.10:6379,192.168.1.11:6379,192.168.1.12:6379,ssl=false,password=My_Super_Secret_Password" />
</providers>
</sessionState>
Hope this help :)

Related

How do I stop using ASPNETDB.MDF in LocalDB?

I implemented ASP.NET Identity and it automatically created ASPNETDB.MDF and aspnetdb_log.ldf in my App_Data folder. I already have the AspNet tables (i.e., AspNetRoles, AspNetUsers, etc) in my SQL Express instance (which is where all my other tables are sitting). As far as I can see, my application is reading and writing membership and role data from the SQL Express database and not ASPNETDB.MDF.
I have set my connectionString in web.config to:
<add name="DefaultConnection" connectionString="Data Source=MyComputerName\SQLEXPRESS;Initial Catalog=MyDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
However, if I remove ASPNETDB.MDF from App_Data, I get the following error when I login:
Exception Details: System.Data.SqlClient.SqlException: One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open user default database. Login failed.
Login failed for user 'MyComputerName\MyUserName'.
Log file 'C:\Users\MyProjectName\App_Data\aspnetdb_log.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously
The error goes away once I add ASPNETDB.MDF back to App_Data.
I have searched all the code in my solution and it makes no reference to ASPNETDB. So why is it still trying to read from it?
I am developing ASP.NET web forms on .Net 4.5.
I was getting exactly the same problem. I discovered that VS annoyingly pulls in config settings from machine.config, which lives outside of the project, in my case in...
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
Identity 2.0 had used the following connection string inside machine.config...
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
to set up a connection for...
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, ........" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
.. (which was also set in machine.config). If you haven't been using Membership then it's fine to do as Lord nick suggests (except just the clear/ will do the job) and simply do the following in web.config...
<connectionStrings>
<clear/>
<add name="DefaultConnection" connectionString="whatever" providerName="System.Data.SqlClient" />
However, if you, like me, previously had Membership running (https://msdn.microsoft.com/en-us/library/6e9y4s5t(v=vs.100).aspx), you will need to comment out or delete the following sections from machine.config...
<!--
<membership>
<providers>
...
</providers>
</membership>
<profile>
<providers>
...
</providers>
</profile>
<roleManager>
<providers>
..
</providers>
</roleManager>
-->
... since all this stuff is no longer needed for AspNet Identity 2.
I also had to add the following into in my web.config:
<modules>
<remove name="RoleManager"/>
</modules>
... as per this answer: Default Role Provider could not be found on IIS 7 running .NET 4
I hope I've saved someone some time. This took me hours and hours to work out.
I had the same Problem where the ASPNETDB.MDF was automatically created, even if I use Asp.Net Identity as the main user management.
I solved it by removing the following line from web.config:
<roleManager enabled="true" />
This one tells ASP.NET to use the older ASP.NET Membership user management, which is not supported by ASP.NET Identity.
It seems you have something like in your web.config
<add name="DefaultConnectionForLocalDb" connectionString="Data Source=(LocalDb)\v11.0;..."; />
In your AccountModels.cs file, you have:
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnectionForLocalDb")
{
}
}
However it should be this way:
<add name="DefaultConnectionForSQLEXPRESS" connectionString="data source=.\SQLEXPRESS;...;" />
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnectionForSQLEXPRESS")
{
}
}
Then you can remove safely DefaultConnectionForLocalDb entry from your web.config and ASPNETDB.MDF from to App_Data.
I don't know if you've figured this out or not but one of the things you can try is: in web.config, connections section, add <Clear/> and then <Remove Name=LocalSqlServer/>
Apparently if you don't change/remove the LocalSqlServe will still try to connect to the aspnetdb.mdf.
You might also think about adding back in the LocalSqlServer and having it point to your SqlExpress or SqlServer.

Log in as domain\user with ActiveDirectoryMembershipProvider

I am using ActiveDirectoryMembershipProvider in ASP.NET WebForms app with this configuration:
<connectionStrings>
<add name="ADConnection" connectionString="LDAP://XXX/OU=XX,DC=XXX,DC=XXX,DC=ac,DC=za"/>
</connectionStrings>
...
<membership defaultProvider="ADProvider">
<providers>
<clear />
<add name="ADProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnection" enableSearchMethods="true" />
</providers>
</membership>
This allows me to log in using user#XXX.XXX.ac.za, but I want to log in using domain\user like one does in Windows. I can't figure out how to do this.
I know I can add attributeMapUsername="sAMAccountName" to the provider definition, which lets me log in simply as 'user' but that's not viable for this client because multiple domains are involved. When I try, I get an error later when I call GetAllUsers() ("Item has already been added. Key in dictionary: 'XXX' Key being added: 'XXX'").
Is there a way to configure ActiveDirectoryMembershipProvider so I can log in with domain\user?

mongodb asp.net session provider: How to set the application name in web.config

Question:
I have a custom MongoDB session provider.
It's configured in the web.config like this
<sessionState cookieless="false" timeout="20" regenerateExpiredSessionId="true" mode="Custom" customProvider="MongoSessionStoreProvider">
<providers>
<add name="MongoSessionStoreProvider" type="MongoSessionStore.MongoSessionStoreProvider" writeExceptionsToEventLog="true"/>
</providers>
</sessionState>
But it throws an exception because the application name is "/"
How can I set the application name ?
I'm not defining any membership provider and I'm not going to define one anytime soon.
Looking at the source code it appears that an applicationName attribute will do:
<add name="MongoSessionStoreProvider" applicationName="myapplicationname"
type="MongoSessionStore.MongoSessionStoreProvider"
writeExceptionsToEventLog="true"/>
You forgot to specify not only application name but connection string to mongodb as well. Full configuration should looks like this:
<sessionState cookieless="false" timeout="20" regenerateExpiredSessionId="true"
mode="Custom" customProvider="MongoSessionStoreProvider">
<providers>
<add name="MongoSessionStoreProvider"
connectionString="mongodb://admin(admin):1#localhost:27020/"
applicationName="ApplicationName"
type="MongoSessionStore.MongoSessionStoreProvider"
writeExceptionsToEventLog="false" />
</providers>
</sessionState>
Some notes:
Application name will be name of collection in mongodb. (database name is AspSessionStage)
If your mongodb installation without --auth you can take out auth info from connection string admin(admin):1#.
This provider does not clear expired sessions (need to run some background job to do it)
You can report any bugs you may find directly to me.

Access to database and connection string

This is my connection string:
<add name="modelConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='D:\Documents and Settings\Dima\My Documents\Visual Studio 2010\WebSites\WebSite10\App_Data\ASPNETDB.MDF';Integrated Security=True;User Instance=True;" providerName="System.Data.SqlClient"/>
It stopped throwing exceptions, but the access to the database doesnt seem to work, cause when i answer the confirmation question, it says it is wrong and wont let me in!!
<membership defaultProvider="MyMembershipProvider">
<providers>
<clear/>
<add
name="MyMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="modelConnectionString"
minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"
enablePasswordReset="true"
maxInvalidPasswordAttempts="1000"
passwordAttemptWindow="4000"
enablePasswordRetrieval="true"
requiresUniqueEmail="false"
passwordFormat="Encrypted"
applicationName="/WebSite10"
/>
</providers>
</membership>
You haven't specified a database name in the connection string - in the top one you are specifying the database file where you should be declaring the database name.
Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
If you're attaching a database file to a local instance of SQL then you need to use the AttachDbFilename property.
Can you not create the connection through the Visual studio server explorer. You can log into the server using the correct credentials then save the correct connection string to the config file.
There is a microsoft knowledge base article! on this.

multiple asp.net membership providers

I have created two custome membership providers that I would like to add to my web.config. The first one would be the default that the asp.net application would use. The second would be called by a WCF service that I have in the same application.
The providers in the membership section of my web.config looks like the following:
<add name="SiteProvider" type="MyNameSpace.SiteProvider, MyNameSpace" ApplicationName="Si2" EnablePasswordReset="true" PasswordStrengthRegularExpression="(?=[\w$#_ ]{8,})(?=.*?\d)(?=.*?[A-z])[\w$#_ ]*" ResetPasswordMinimumLength="8" ResetPasswordPattern="USL9SLU9SLU9SLLLL" ResetPasswordAllowDuplicateCharacters="false" />
<add name="WCFProvider" type="MyNameSpace.WCFProvider, MyNameSpace" ApplicationName="Si2" EnablePasswordReset="true" PasswordStrengthRegularExpression="(?=[\w$#_ ]{8,})(?=.*?\d)(?=.*?[A-z])[\w$#_ ]*" ResetPasswordMinimumLength="8" ResetPasswordPattern="USL9SLU9SLU9SLLLL" ResetPasswordAllowDuplicateCharacters="false" />
I receive the error "Item has already beed added. Key in dictionary: 'SiteProvider' Key being added: 'SiteProvider'" any time I browse to the site.
This doesnt make sense to me sense they have unique names. If i remove the second provider the site is browseable.
Any help on adding this second provider would be appreciated.
The simplest solution would be to move the webservice into a different project.
Also, make sure you call <clear/> before adding providers and include a default one...
<membership defaultProvider="Siteprovider">
<providers>
<clear/>
<add name="SiteProvider" type="MyNameSpace.SiteProvider, MyNameSpace" ApplicationName="Si2" EnablePasswordReset="true" PasswordStrengthRegularExpression="(?=[\w$#_ ]{8,})(?=.*?\d)(?=.*?[A-z])[\w$#_ ]*" ResetPasswordMinimumLength="8" ResetPasswordPattern="USL9SLU9SLU9SLLLL" ResetPasswordAllowDuplicateCharacters="false" />
<add name="WCFProvider" type="MyNameSpace.WCFProvider, MyNameSpace" ApplicationName="Si2" EnablePasswordReset="true" PasswordStrengthRegularExpression="(?=[\w$#_ ]{8,})(?=.*?\d)(?=.*?[A-z])[\w$#_ ]*" ResetPasswordMinimumLength="8" ResetPasswordPattern="USL9SLU9SLU9SLLLL" ResetPasswordAllowDuplicateCharacters="false" />
</providers>
</membership>
You could try the solution in this forum post: http://forums.asp.net/p/1112089/1714276.aspx
Have you tried putting a separate web.config file in the webservice folder? I doubt that it will work, but it might be worth a try.

Resources