Showing posts with label Sitecore. Show all posts
Showing posts with label Sitecore. Show all posts

Wednesday, 13 October 2010

Developing a custom MembershipProvider

there are some samples out there on the net just showing what the default providers already do: Using an SQL DB as the source. They even concentrate on the SQL stuff, instead on the MembershipProvider implementation.

So here we go, a very simple example:

First create a new class that inherits from the abstract MembershipProvider class.

public class MyMembershipProvider : 
System.Web.Security.MembershipProvider

We just need to implement two simple methods and one property to get started:

public override void Initialize(string name, 
System.Collections.Specialized.
NameValueCollection
config)
{
//base.Initialize(name, config);
//read you config settings and
        //do whatever you need here
}



If you connect to some database init your db settings here, or read your web service settings if you plan to consume a web service later.


Next, we implement the ValidateUser method:


public override bool ValidateUser(string username, 
string
password)
{
if (username == "user" && password == "pass") return true
;
return false
;
}


This is just a sample to let you see how it works, of course you are going to implement here the validation to you backend, be it LDAP, a DB, web services or whatever.


The last thing is the property Name. As long as I did not implement this the login failed, throwing an exception.


    public override string Name
    {
       
get
        {
           
return "SampleMembershipProvider";
        }
    }


Basically this is the name you refer to in the configuration.


Now on to the web.config. We add the MembershipProvider and declare that /images may be accessed by everyone (*), but /locked only by authenticated users. And we need to configure the authentication:


<authentication mode="Forms">
   <
forms name=".ASPXAUTH"
loginUrl="~/Default.aspx"
protection="Validation"
timeout="999999"
    />
</
authentication>



<membership defaultProvider="SampleMembershipProvider" 
userIsOnlineTimeWindow="15"
>
<
providers
>
   <
add name="SampleMembershipProvider"
type="MyMembershipProvider"
/>
  </
providers
>
</
membership
>
</
system.web
>
<
location path="images"
>
  <
system.web
>
     <
compilation debug="true"
/>
      <
authorization
>
         <
allow users="*"
/>
       </
authorization
>
     </
system.web
>
  </
location
>
  <
location path="locked"
>
    <
system.web
>
      <
compilation debug="true"
/>
      <
authorization
>
        <
deny users="?"
/>
      </
authorization
>
    </
system.web
>
  </
location>



Now we place the asp:Login control on our default.aspx page (we told web.config that this is the login page).


<asp:Login RememberMeSet="True" runat="server"
 ID="logincontrol"
MembershipProvider="SampleMembershipProvider"
DestinationPageUrl="~/locked/securedpage.aspx">
</asp:Login>


Thats it. Have fun.

Tuesday, 9 June 2009

Empty strings are not allowed

We migrated the developer environment from Sitecore 5 to Sitecore 6.0.2.

Basically everything went straight forward. Its not comfortable but ok. Some APIs have changed, that business as usual.

However – one problem took a while to be solved – the “Empty strings are not allowed - Parameter name: fieldName”.

We tried many different things to solve this – updating templates, verifying web.config, debugging and so on. Then we discovered – that one of our colleagues did not have the problem.

So I went to user manager, selected my profile, clicked on “Reset Settings” logged in again – and there – it worked.

In the meantime I learned from Sitecore Support that there is even a tool to fix the problem – you just need to open a case,….

Monday, 6 April 2009

Sitecore Item:Saving

Sitecore has a wonderful system to add your code to specific events. I have used Item:Saving to summarize some content fields into one specific field which we used for indexing. That worked great.

Recently I received a request to do some more sophisticated checks on some fields and deny saving when something does not fit.

I remembered my OnItemSaving method and added some code to it, but when something went wrong, the message box telling the error came twice.

After some debugging I found that the code really gets called twice. But why?

Try yourself – check your web.config and set <events timingLevel="custom"> to <events timingLevel="high">. Modify an item, save it and check your log file:

Event started: item:creating
Event ended: item:creating. Elapsed: 0.00838095344520044
Event started: item:created
Event ended: item:created. Elapsed: 0.00502857206712026
Event started: item:versionAdding
Event ended: item:versionAdding. Elapsed: 0.00474920695228025
Event started: item:saving
Event ended: item:saving. Elapsed: 0.00586666741164031
Event started: item:saved
Event ended: item:saved. Elapsed: 419.566732643395
Event started: item:versionAdded
Event ended: item:versionAdded. Elapsed: 0.00474920695228025
Event started: item:added
Event ended: item:added. Elapsed: 0.00502857206712026
Event started: item:saving
Event ended: item:saving. Elapsed: 0.00419047672260022
Event started: item:saved
Event ended: item:saved. Elapsed: 98.5195045739054

Any Idea? :-)

Friday, 21 November 2008

Hotel Room 404

Everyone knows people who are some kind of superstitious. However, I do not care about black cats, the number 13 or what ever.

I just had a good laugh when I received the key for my hotel room.

image

And for a superstitious (web) developer - this must be the worst room to stay :-)

Friday, 7 November 2008

Sitecore Level 2

Last week I passed Sitecore Level 2 certification. So let's continue with work on Sitecore,....

Thursday, 4 September 2008

Sitecore getting back to standard?

So you changed the value of a field and want to revert to standard? Thats even more important if you did some layout changes, but need to get back to the template's standard?

Go to the Versions tab, there you will find the "Reset" button.

It saved my day!

Wednesday, 27 August 2008

Custom Languages and Sitecore

There is of course one detail you have to consider:

Writing out the content language needs a translation. But I guess you thought of that when you where creating the rendering for the meta tags.

Wednesday, 21 May 2008

Sitecore and Languages

My customer uses translation services and employees to do all the necessary translation work. They have a very good web application for that and utilize those translations on their current homepages.

To shorten that story a little bit, every text is represented using "webkeys" in the form [key=KeyName] and is then translated "on the fly" into the text and language necessary for display.

Not all developers know of ISO Standards, or are able to use the correct one. So for some languages, instead of the Language Identifier, the Country Code has been taken.

Now two options:

1) In Sitecore add the correct language, create either some Sitecore Items to tranlsate from Correct to Wrong Language Identifier, add code to perform the translation, eventually add a cache and manage it, go to the translation db and request translations for your keys.

2) Create Custom Cultures, e.g. reigster the wrong Language Code in the system, in Sitecore add the Language with the "wrong" ISO code, no other changes, no cache, no additional code.

To work with option two - download Sitecore`s Language Registration Tool and register the Language Code.

Sitecore Language Registration tool

In the above screen shot you can see that I registered  a language "cz", so I now can use the language identifier cz in Sitecore.