CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

February 2004 - Posts

  • Forms authentication without a (visible) form

    A user can browse your web-pages anonymous or as an authenticated user. This is set in the web.config of your site.

    <authorization>
       <allow users="*" /> <!-- Allow all users --
    >
    </authorization>

    By default every user can browse any page. If you want to know who is requesting, deny the access to unknown users

    <authorization>
      <deny users
    ="?"/>
    </authorization>

    Now every user has to be authenticated to view a page. You set authentication on a page basis by adding sections to the web.config

    <location path="AuthRequired.aspx">
      <system.web
    >
        <authorization
    >
         
    <deny users
    ="?"/>
        </authorization
    >
      </system.web>

    </location>

    Take a look at the asp.net forums app to get an idea how to use that. In a forum everybody can browse and read the posts but you have to be authenticated to (react on a) post.

    ASP.NET has several ways of authentication built in: Windows integrated, Passport and forms. Using forms authentication the user is forced to a login page before visiting a page. This login page is also set from the web.config file

    <authentication mode="Forms">
      <forms name
    =".MyAuthCookieName"
          loginUrl="AuthenticateHere.aspx"

          protection
    ="All"
          timeout="30"
    />
    </authentication>

    On the login page the user enters a username and password which is supposed to be validated against the one or the other. When approved the code will set an authorization cookie. With this cookie the user can visit all pages shielded off without having to log in again and again. When the code issues a persistent cookie the login will persist over sessions. (Remember me).

    But nothing forces you to pop up a form. Asp.Net doesn't care as long as the cookie is set. You could do this for instance

    private void AuthenticateHere_Init(object sender, System.EventArgs e)
    {
       if (isValidAddress(Context.Request.UserHostAddress))
       {
          System.Web.Security.FormsAuthentication.SetAuthCookie(Context.Request.UserHostAddress, false
    );
          string redirectUrl = Page.Request.QueryString["ReturnUrl"
    ];
          if (redirectUrl != null
    )
          {
             Page.Response.Redirect(redirectUrl);
             Page.Response.End();
          }
       }
    }

    Right in the start of the pages lifecycle, in the init event of the page the cookie is set. Why the user deserves an authentication cookie is up to you, here I use a custom method IsValidAdress. After setting the cookie the response is ended and the app is redirected to the page requesting the authentication. FormsAuthentication passes the url in the querystring. In the browser toolbar you will see the Authenticate page being hit, on a succefull authentication the visual page itself jumps directly to the page requested in the querystring.

    The nice thing about forms authentication that it is completely set from the web.config. This way you can hook your own authentication into an existing app. Like asp.net forums.

    Peter

  • Why localhost may not be a good sqlserver (Cannot generate SSPI context)

    Setting up the asp.net forums app on my local XP pro machine took some puzzling. The setup proposes localhost as sqlserver. Running the app resulted in a Cannot generate SSPI context error. There is a MS support article on this HOW TO: Troubleshoot the "Cannot Generate SSPI Context" Error Message. This article is interesting but tries to cover to much. In my scenario it boiled down to sql server being unable to set up a trusted connection to Localhost.

    This was the connectionstring (found in the web.config)

    <add key="connectionString" value="server=localhost;Trusted_Connection=true;database=AspNetForums" />

    Changing the server part of the connectionstring to the name of my machine

    <add key="connectionString" value="server=MyComputer;Trusted_Connection=true;database=AspNetForums" />

    resulted in a proper connection and got the app up and running

    Peter
  • My neighbour started blogging

    My neighbour started blogging. Now we can wave at each other from our window, chat on MSN and exchange messages using that .text server on the other side of the globe. Isn't technology great ?

    Peter

  • Codezone magazine

    Last DotNed meeting we were handed out #1 of codezone magazine

    >> enter...

    The magazine looks very familiar. It is (graphicaly) styled like a Microsoft Developer product and quotes itself as the latest “cool tool” from MS. Inside the usual adds, a couple of technical articles, some techno lifestyle babbel and last but not least information on European .net user groups.

    Not bad... not great either. It's for free..

    Peter

     

  • Crystal reports roundup

    Getting Crystal Reports to work on a webserver is a bumpy road. Solving parts of the puzzle is usualy worth a (well read) blog post Here's a roundup of my main problems with Crystal :

    Peter

  • format

    Handling strings in .NET can be very expensive, one of the most down to earth recomendations of code profilers often deals with this. In a response on a profiling post (sorry, lost the link..) Frans mentions the good perfomance of the string.format method. AFAIK ( I'm no C guy) in “classical MS languages“ formating strings was mainly done when writing them to output, in the parameters of a print function. In Delphi the ability to format a string has been in the core of the framework ever since the start, the class library houses a global fomat function which looks a lot like the .NET method.

    According to my myth format leads to faster and clearer code. Take this:

    string.Format("{0:D} {0:T} : Received {1}.",DateTime.Now, any);

    The parameters passed can be used more than once. Applying a different format on every usage. Of parameter 0, being Now, first the date and than the time is injected into the string.

    Not using the format method the code would have looked like this

    return DateTime.Now.Date.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + " : Received " + any + ".";

    The latter doesn't look that good but but some profiling learns that it does perform better. There goes my myth..

    Peter

  • for a while

    for (int i= 1; i < 10; i++)
       textBox1.Text+= i.ToString();

    for (int i= textBox1.Text.Length; textBox1.Text.Length < 20; i++)
       textBox1.Text+= i.ToString();

    for (;textBox1.Text.Length < 40;)
       textBox1.Text+= '#'
    ;

    while (textBox1.Text.Length < 60)
       textBox1.Text+= '$';

    The textbox (started as ++) will contain ++1234567891112131415###################$$$$$$$$$$$$$$$$$$$$ (what else).

    Thanks to Frans on do while.

    Peter

     

     

  • do while, do not repeat

    In C# you can express a lot using with a minimal set of statements. One of the active goals of the C# team is to keep the language as simple and small as possible. Another (small) example of this is the flexbility of the while statement. Take this snippet of code

    string dirName = @"C:\USR";
    string fileName = "*.cs"
    ;
    bool fileMissing = true;

    while(fileMissing)
    {
       fileName = myFiddleFuction(fileName);
       fileMissing = Directory.GetFiles(dirName, fileName).Length == 0;
    }

    This can also be expressed as :

    string dirName = @"C:\USR";
    string fileName = "*.cs";

    do
    {  
       fileName = myFiddleFuction(fileName);
    }
    while(Directory.GetFiles(dirName, fileName).Length == 0);

    The control variable fileMissing, which had to make sure the loop was executed at least once, is gone. If you ever worked with a language like Pascal (Delphi) you'll propably recognize the repeat statement.

    Peter

  • Dutch user groups meeting

    The Dutch .NET user group dotNed will meet the evening 26th of february in Utrecht. Admitance is free to everybody.

    The Dutch usergroup SDGN (covering .NET, Delphi, Java, Visual object and Clipper) will meet the whole monday the 15th of March in Ede. That's members only, memebership is open to every professonal developer.

    See you there ?

    Peter

  • Are you sure ?

    Are you sure about what you'r blogging about ? What about the disclaimer on Omri Gazitt's blog :

    “The content of this site are my own personal opinions and do not represent my employer's view in anyway. In addition, my thoughts and opinions often change, and as a weblog is intended to provide a semi-permanent point in time snapshot you should not consider out of date posts to reflect my current thoughts and opinions.“

    beautifull,

    Peter

  • (Small) differences between C# and vb.net in code completion

    Last weeks I've been doing some things in vb.net. Alas, you cannot mix languages within one assembly so I had no choice :> (No flames intended)

    When it comes to codecompletion (I am a lazy typer) there a two notable differences in the behaviour of the VB.net editor versus the C# one. First of all VB takes a significant larger amount of time to build the list. What is harder to get used to is how to pick a member from the list. I was used to do this with the <enter> key in C#. This works in the VB editor but you get an extra line break for free. Using the <Tab> key gives the correct result in VB. And also works in the C# editor.

    Peter

  • Minimizing apps, (sometimes) in the system tray

    Several apps, like Outlook 2003 and FeedDemon, can minimize into the system tray, the right corner of the Windows taskbar. This is handy, they are allways avaliable but only take up real task-bar space when maximized. On my machine this only works under certain conditions. When I click the minimize button they're off into the system tray, when I press <Alt ><Esc> they minimize into the task bar. Is this a bug, a feature or a setting ?

    Peter

  • Another article on XML datasets

    On the site of the SDGN is a new article on XML-datasets. It starts with serializing plain objects to XML, covers the basics of ado.net and ends with bringing the two together. Quite a firehose.. In English.

    Coming march 15th I will do a presentation on the SDGN meeting. in the material. And another one on editing live data in a datagrid/list. All in Dutch and SDGN members only....

    Peter

     

  • Virtual tourism : meet the Puny Pickers

    This one is not about .Net. ...

    First of all apologies to Scott : apologies for the abuse language. Let's blog and take the bumps in .Text for granted as being part of the game. Let's also blog on side steps, aka of-topic. To me blogging is writing about what I've been doing with web technology, but also about what I found out there on the web. Which is more than the .net pages I Google. This post is on another side of American culture.

    I have not seen very much of the US yet, been to LA once but mainly saw the convention centre. Been to Manhatten twice, but that's said to not be be the real America. The internet gives me the opportunity to be a virtual tourist. This weekend, looking for more vocal harmonies, I surfed the web on old country music and bluegrass search keys. Numerous samples of historical material popped up. None of them were very enjoyable as most of them were no longer than a couple of seconds after which you are directed to Amazon.com. Far more rewarding was the material on contemporary artists playing “old-style". There is a quite an online community. Many a link leads nowhere but several do have online music.

    I would like to share one of the sites I met with you. The Puny Pickers look like coming straight from an US travel brochure. Their site offers two online songs. Real woman drive trucks is an instrumental on banjo and mandolin. Sounds corny ? Actually it does sound wild, authentic and very energizing. The twist is in the second song I'll fly away which does have vocals. The performers are a bunch of kids singing "old-style" razor-harmonies. Out of this world ? Could be, but also right here in the world of the web.

    Peter

  • Xs4all and mail filters

    This is old news but still worth mentioning. The famous dutch provider xs4all has been offering to scan you email for virusses for some time. But it was not for free and being typically Dutch I turned down their offer. Last week I found out that the service is free since the 1st of january. So I gave it a try during last week's Mydoom shower. Never had a better inbox. So if you have an xs4all mail acoount and didn't know yet...

    Xs4all also has a spam scanner service. Which always was for free. The problem with this service that it is (still) to strict. Everyday I have to skim my spamboxes to find my newsletters. Which is not that much of a problem since I have my little DevMail.Net tool. More on DevMail.Net in a later post. It really is a great tool but like every piece of software, the docs could be (a little) better.

    Peter

More Posts Next page »