BlogEngine.Net + Medium Trust + Surftown

Posted on November 24, 2010 07:06 by mystiqu

If you are using BlogEngine.Net on a site hosted by Surftown you have probably noticed that you get prompted with a credentials popup every now and then. This is because Surftown are running their applications in Medium Trust, which means you (NETWORK_SERVICE to be exact) don´t have any write access to the App_Data folder. Turn off “custom errors” on web.config and you´ll see a permission denied error.

Since the application is running under the NETWORK_SERVICE user we must give that user write privileges to the App_Data folder and all subfolders and files. Luckaly, surftown are nice enough to allow this and has also provided a script you can run to give the NETWORK_SERVICE user write access to any folder you wish. However, for security reasons you should keep the write access privileges to a minimum!

You can find the script here.
For all of you that don´t speak swedish – here comes a translation.

1. Create an aspx file and name it to change.aspx
2. Add the code below
3. Upload the file to a domain other than the domain where you keep your blog, e.g. create a temporary sub domain
4. Verify the ASP.NET version is set to 2.0 on the surftown control panel
5. Surf to the change.aspx page
6. You will be prompted by a credentials box. Supply your ftp credentials.
7. Done! Hopefully it worked and your application should run smoothly from now.

The Script

Download it here

   1:  <%@ Page Language="C#" %>
   2:   
   3:  <%@ Import Namespace="System.Security.AccessControl" %>
   4:  <%@ Import Namespace="System.Security.Principal" %>
   5:  <%@ Import Namespace="System.IO" %>
   6:   
   7:  <script runat="server">
   8:          protected void ChangeBT_Click(object sender, EventArgs e)
   9:          {
  10:                  // Get path from TextBox
  11:                  string path = PathTB.Text;
  12:                  
  13:                  // The account which should be granted access
  14:                  NTAccount acc = new NTAccount(@"NT AUTHORITY\NETWORK SERVICE");
  15:   
  16:                  // Rights which should be changed
  17:                  FileSystemRights rightName = FileSystemRights.Modify;
  18:                  AccessControlType right = AccessControlType.Allow;
  19:   
  20:                  // Inherit to all subdirectories and files
  21:                  InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
  22:                  PropagationFlags pFlags = PropagationFlags.InheritOnly;
  23:   
  24:                  // Get current filesecurity object
  25:                  DirectorySecurity security = Directory.GetAccessControl(path);
  26:   
  27:                  // Create new rule
  28:                  FileSystemAccessRule rule = new FileSystemAccessRule(acc, rightName, iFlags, pFlags, right);
  29:   
  30:                  // Add new rule to the security object
  31:                  security.AddAccessRule(rule);
  32:   
  33:                  // Update file access control
  34:                  Directory.SetAccessControl(path, security);
  35:   
  36:                  UpdatedLB.Visible = true;
  37:          }
  38:  </script>
  39:   
  40:  <html>
  41:  <body>
  42:          <form id="Form1" runat="server">
  43:          <p>
  44:                  Path: (d:\hshome\username\yourdomain\foldername)<br />
  45:                  <asp:TextBox ID="PathTB" runat="server" Width="250px" />
  46:          </p>
  47:          <p>
  48:                  <asp:Button ID="ChangeBT" runat="server" Text="Change Permissions" onclick="ChangeBT_Click" />
  49:          </p>
  50:          <p>
  51:                  <asp:Label ID="UpdatedLB" runat="server" Text="Permissions changed!" Visible="False" />
  52:          </p>
  53:          </form>
  54:  </body>
  55:  </html>

Creating widgets in BlogEngine.Net

Posted on November 23, 2010 06:30 by mystiqu

The first thing I wanted for my BlogEngine powered site was to write a widget.
Since I haven´t dived very deep into the BE.NET code I´ll start with a simple widget that displays some profile information and a small image, like an avatar.
Basically, a classic "About Me" widget that will replace the TextBox widget that comes along with the source.

The basics

A widget consists of two elements; a widget control (ordinary user control) containing the UI and logic, and an edit control which both must inherit from WidgetBase and WidgetEditBase. They has to be placed in the widgets folder in the BlogEngine.Net project for it to work. BE.NET then uses reflection to dynamically load new widgets upon compile time. Every widget also has it´s own "storage" for storing widget-related data. Just call WidgetBase.GetSettings() to retrieve a StringDictionary and WidgetEditBase.SaveSettings(StringDictionary d) to save. The widget framework will take care of all the serialization and deserialization for you.

Step 1

Start with creating a new folder - lets call it AboutMe - in the widgets folder.


Then add 2 usercontrols; widget & edit and make them inherit from WidgetBase and WidgetEditBase


public partial class widgets_AboutMe_Widget : WidgetBase
{
}

public partial class widgets_AboutMe_edit : WidgetEditBase
{
}

Now we have to override some properties/functions in the widget control:


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

    public override bool IsEditable
    {
        get { return true; }
    }

    public override void LoadWidget()
    {
        //Nothing to do
    }

The Name property decides the name for the property and it _must_ be the same as the folder name.
The IsEditable property tells the widget engine whether or not the widget is editable.
The LoadWidget() function is a widgets equivalent to Page_Load.

If you compile it now you should be able to add your new widget to the sidebar.
Dont forget to add the WidgetZone in your master page.


<blog:WidgetZone ID="WidgetZone1" runat="server" ZoneName="be_WIDGET_ZONE" />

More...


The birth of yet another blog

Posted on November 21, 2010 02:05 by Admin

Yet another blog has come to life. Blood, sweat, tears and thousands of man hours was required to give birth to this magnificent blog... just kidding,  downloaded BlogEngine.Net, a nice theme from NodeThirtyTree, compiled and was good to go in a few hours; Sweet Cool

Anyway - for quite some time I´ve wanted to start a blog. Not the classic "write-about-myself-blog", but a blog about pretty much anything that are of interest for me. As I am a developer, most of the posts are probably gonna be about some technical topic - as it´s my absolute favourite topic. It´s intended solely for myself as an archive of technical information, achievements, howto's, how I solved certain problems and much more. However, if someone find a particular post interesting, helpfull... or just plain awefull - feel free to comment Smile

About me

So, who am I then?
Simply put - a 30+ nerd with love for programming, sports and fantasy books... and any book by Ken Follet.
When I´m not learning some new technology (MVC at the moment), reading a good book or out running, I´m probably enjoying a nice single malt scotch - smoky as hell! 
Well, thats a little bit about me - happy reading!

 

//Mike>