When SimpleMembershipProvider is not the default provider - membership-provider

When you have multiple membership providers configured, like:
<membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add name="UmbracoMembershipProvider" type="umbraco.providers.members.UmbracoMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed" />
<add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>
</providers>
</membership>
Then let's say, for a particular section in the site I want to use SimpleMembershipProvider for auth (the rest of the site will use the default provider i.e. UmbracoMembershipProvider) ... Therefore I need to initialize SimpleMembership with:
WebSecurity.InitializeDatabaseConnection(
Constants.Membership.ConnectionStringName,
Constants.Membership.TableName,
Constants.Membership.UserIdColumnName,
Constants.Membership.UserNameColumnName,
false);
But it seems the above code only works when SimpleMembershipProvider is the defaultProvider. I can't see a way to specify the provider name I want to use? Is this even possible? Or does simplymembership assume you have one membership and role provider defined?

SimpleMembershipProvider is an implementation of ExtendedMembershipProvider. Although WebSecurity will work with any implementation of the extended provider it has some questionable coupling to SimpleMembershipProvider if used.
Basically SimpleMembershipProvider requires initialisation via WebSecurity, otherwise it will throw an exception when the extended membership interface is used. If not initialised then it wraps the default ASPNET provider and can be used with the original MembershipProvider Interface and old schema.
The initialisation routine only initialises the default providers, provided they can be cast to the simple provider implementations. So yes you cannot use SimpleMembershipProvider or SimpleRoleProvider unless they are configured as the default instances.
There's some more information about the limitations of SimpleMembershipProvider here.
There is a work-around. You can use reflection to temporarily swap the provider to the default during initialisation and then switch it back. See BetterMembership.Net for an example of doing exactly that. This library extends SimpleMembershipProvider specifically for use in multi-provider systems.
membershipProviderField = typeof(Membership)
.GetField("s_Provider", BindingFlags.NonPublic | BindingFlags.Static);
var originalMembershipProvider = membershipProviderField.GetValue(null);
membershipProviderField.SetValue(null, mySimpleMembershipProvider);
WebSecurity.InitializeDatabaseConnection(...)
membershipProviderField.SetValue(null, originalMembershipProvider);

Related

'The ADO.NET provider with invariant name 'Devart.Data.Oracle' is either not registered in the machine or application config file

Ok, there already is a similar issue. It is, however not exactly the same and the solution I got was not derived from the other issue's solution.
Here's my Web.Config setup:
<connectionStrings>
<add name="ADOEntities" connectionString="metadata=res://*/ADOModel.csdl|res://*/ADOModel.ssdl|res://*/ADOModel.msl;provider=Devart.Data.Oracle;provider connection string="User Id=dbUser;Password=*****;Server=oracleserver;Persist Security Info=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Everything runs fine on my machine (sic), but when I tried to set the ws up on the quality server, I got the error on the title.
I got it working by following the steps in this ADO.NET link. Particularly,
You need to remove the defaultConnectionFactory registration and to add the Entity Framework provider registration by registering it in the entityFramework section
So the line defaultConnectionFactory must go
<entityFramework>
<providers>
<provider invariantName="Devart.Data.Oracle" type="Devart.Data.Oracle.Entity.OracleEntityProviderServices, Devart.Data.Oracle.Entity.EF6, Version=9.6.696.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
</providers>
</entityFramework>
Then, add the System.Data section. In my case it looks like this:
<system.data>
<DbProviderFactories>
<remove invariant="Devart.Data.Oracle" />
<add name="dotConnect for Oracle" invariant="Devart.Data.Oracle" description="Devart dotConnect for Oracle" type="Devart.Data.Oracle.OracleProviderFactory, Devart.Data.Oracle, Version=9.6.696.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
</DbProviderFactories>
</system.data>
If that still doesn't do the trick for you -- And it didn't for me -- try adding the following line to your context class:
[DbConfigurationType(typeof(Devart.Data.Oracle.Entity.OracleEntityProviderServicesConfiguration))] //Add this line
public partial class ADOEntities : DbContext
You might want to create a partial class, incase you're using ADO, Devart, or other auto-generated entity model, to avoid having this piece of code in an auto-generated class

Enabling CORS through Web.config vs WebApiConfig and Controller attributes

There seems to be two functionally different ways to enable cross-origin request sharing in Web API 2.
One is to import System.Web.Http.Cors, decorate a controller with the EnableCors attribute and to write config.EnableCors() in the WebApiConfig:
[EnableCors(origins: "http://111.111.111.111", headers: "*", methods: "*")]
public class GenericController : ApiController
{
// etc.
The other is to modify the Web.config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://111.111.111.111" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
Is there a functional difference between these two different approaches? Which one is correct - don't these accomplish the same thing? If both methods are used to enable CORS, will things blow up?
If you add the headers to the web.config, every request that is served by that application will include the specified headers. This method is supported at the web server level and doesn't depend on config.EnableCors() being executed. You can use that method to add any HTTP header you want.
On the flip side, the EnableCors attribute requires WebAPI and you need to add some code to make it work. To the end user, the result is the same.
As for which way is better? I've liked keeping those settings in the application code by using the attribute so these settings are obvious to future developers. Depending on your needs, you may want to look into a abstract CorsApiController which your main ApiControllers could inherit to deliver the same CORS headers over and over. But this method won't work if the CORS headers need to vary from controller to controller or from action to action.

MVC Custom Membership Provider

I have a custom membership provider that I've specified to be used in my web.config file for an MVC3 project. I assumed that it would be used in place of the built in membership provider since I'm specifying its use like so:
<system.web>
<membership defaultProvider="CustomMySQLMembershipProvider">
<providers>
<clear />
<add
connectionStringName="dbConn"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="25"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
applicationName="/jp2012"
name="CustomMySQLMembershipProvider"
type="NameSpace.CustomMySqlMembershipProvider" />
</providers>
</membership>
</system.web>
Do I have to do something in my code to get it to override the default membership provider? Right now I can access it like this:
CustomMySqlMembershipProvider membership =
(CustomMySqlMembershipProvider)Membership.Providers["CustomMySQLMembershipProvider"];
Is there a way that I can get it to be automagically overriden using the web.config file so I don't have to hardcode my implementations name? Something more like this:
MembershipProvider membership = new MembershipProvider();
Any ideas? Let me know if you need more information.
Your Membership provider is the default provider in your case. You don't need to explicitly cast the provider unless you have additional functionality in your provider.
For example when you call Membership.GetUser() it will call your membership prover's GetUser(...) method.
The issue was actually with how I declared my membership. The line
type="NameSpace.CustomMySqlMembershipProvider"
did not specify the assembly that my custom membership provider was defined in. I right clicked on my project, selected properties, and got the assembly name from the field marked Assembly name.
So if my assembly name was AName, then this line should have been
type="NameSpace.CustomMySqlMembershipProvider, AName"
Thank you #Eranga for attempting to help me out.

where can i find SharedCache with fluent nhibernate getting started guid

where can i find SharedCache with fluent nhibernate getting started guid
There is very little documentation I've found for getting SharedCache working with NHibernate, let alone doing it fluently. However, it's not a hard process:
You need to tell NHibernate you're using SharedCache as a cache provider. Another Stack Overflow answer gives a sample implementation for Fluent NHibernate. The key bit is the Cache function, except here you will want to use NHibernate.Caches.SharedCache.SharedCacheProvider instead of NHibernate.Cache.HashtableCacheProvider.
As an aside, to configure it non fluently you use something like this in your configuration:
<add key="hibernate.cache.provider_class" value="NHibernate.Caches.SharedCache.SharedCacheProvider, NHibernate.Caches.SharedCache" />
Or, if you're configuring via Spring.NET with the rest of your NHibernate configuration entries:
<entry key="cache.provider_class" value="NHibernate.Caches.SharedCache.SharedCacheProvider, NHibernate.Caches.SharedCache" />
You will then need to add web.config / app.config entries for the cache as you would when using SharedCache without NHibernate, for example:
<configSections>
<section name="indexusNetSharedCache" type="MergeSystem.Indexus.WinServiceCommon.Configuration.Client.IndexusProviderSection, MergeSystem.Indexus.WinServiceCommon" />
</configSections>
<indexusNetSharedCache defaultProvider="IndexusSharedCacheProvider">
<servers>
<add key="myServer1" ipaddress="127.0.0.1" port="48888"/>
</servers>
<providers>
<add name="IndexusSharedCacheProvider" type="MergeSystem.Indexus.WinServiceCommon.Provider.Cache.IndexusSharedCacheProvider, MergeSystem.Indexus.WinServiceCommon" />
</providers>
</indexusNetSharedCache>
You can find plenty more information about configuring a SharedCache client on their website, although be sure to configure each server too.
Hope this helps - I haven't been able to fully test this, so I may've made a mistake somewhere.

Set connectionstring for Membership Service via code

I have an ASP.NET web project and a membership provider configured via my web.config. Its fully working so no problem there.
We have an older system with a lot of users and I would therefor like to create a class library that can create users in this ASP.NET project but since its a class library it cannot have its own app.config-file.
is it possible to set all this information via code somehow?
<membership defaultProvider="ShidMembershipProvider">
<providers>
<clear/>
<add name="ShidMembershipProvider" type="SundaHus.AspNet.Membership.ShidMembershipProvider" connectionStringName="ShidConnectionString" enablePasswordRetrieval="true" enablePasswordReset="true" requiersQuestionAndAnswer="true" applicationName="ECB3-development" minRequiredPasswordLength="5"/>
</providers>
</membership>
You have a custom membership provider it looks like? This connects to your own custom database? You should be able to just point to that database for your code. Or, if you just inherit the functionality from the base class, you can also try overriding the Initialize method, look for the connection string, and change the value to something else.

Resources