Quantcast
Channel: MetroStar Systems Blog » ASP .Net
Viewing all articles
Browse latest Browse all 10

Storing Credentials in a SharePoint Property Bag

$
0
0

I recently posted about storing credentials in the Secure Store Service, and based on a few comments, I figured I should also post about storing credentials in the property bag. For this post I’m just going to keep it real simple and provide a few notes and the code (please don’t hesitate to ask for clarification)…

For this example I followed these general steps:

1. Created an Empty SharePoint Project
2. Mapped the Layouts folder in the solution
3. Added an Application Page to the mapped Layouts folder
4. Added some markup and code to the custom application page

The way the custom page works is that on Page Load it checks the Root SPWeb Property Bag for our 2 custom properties (custom_username and custom_password). If the properties exist the username is loaded into the TextBox (the password is not due the TextBox being in Password mode). From there if you click the Save button, the properties are either added or updated if they already exist. And that’s essentially it. Another thing to note is when the page loads I check to see if the current user is a site collection administrator to prevent unwanted users from accessing/changing the properties.

Below is what my markup looks like in the ASPX page:

<asp:Panel ID="pnlProperties" runat="server">
    <div>
        Username: <asp:TextBox ID="tbxUsername" runat="server" />
        Password: <asp:TextBox ID="tbxPassword" runat="server" TextMode="Password" />
    </div>
    <div>
        <asp:Button ID="btnSave" runat="server" Text="Save" />
    </div>
</asp:Panel>
<div>
    <asp:Label ID="lblMessages" runat="server" />
</div>

And finally below is the code behind (make sure you add a using statement for Microsoft.SharePoint.Utilities):

/// <summary>
/// Protected method to run at Page_Load event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
    btnSave.Click += new EventHandler(btnSave_Click);
    if (!IsPostBack)
    {
        LoadForm();
    }
}
 
/// <summary>
/// Private helper method to load the property values into the text boxes.
/// </summary>
private void LoadForm()
{
    /* Display the panel if the current user is a Site Collection Admin */
    if (SPContext.Current.Web.CurrentUser.IsSiteAdmin)
    {
        tbxUsername.Text = GetValueFromPropertyBag("custom_username");
        tbxPassword.Text = GetValueFromPropertyBag("custom_password");
    }
    /* Otherwise hide the panel and display a message to the user */
    else
    {
        pnlProperties.Visible = false;
        lblMessages.Text = "You do not have the proper level of access to manage these properties.";
    }
}
 
/// <summary>
/// Private accessor method to get a property from the Property Bag based on the specified key.
/// </summary>
/// <param name="key">The string value of the key.</param>
/// <returns>Returns a string representation of the property value.</returns>
private string GetValueFromPropertyBag(string key)
{
    string value = null;
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPPropertyBag properties = SPContext.Current.Site.RootWeb.Properties;
            if (properties.ContainsKey(key))
            {
                if (properties[key] != null)
                    value = properties[key].ToString();
            }
        });
        return value;
    }
    catch (Exception ex)
    {
        lblMessages.Text = ex.ToString();
        return value;
    }
}
/// <summary>
/// Private Event Handler to update the property bag based the username and password.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        /* If the form is complete update the properties */
        if (!String.IsNullOrEmpty(tbxUsername.Text) && !String.IsNullOrEmpty(tbxPassword.Text))
        {
            SPPropertyBag properties = SPContext.Current.Site.RootWeb.Properties;
            if (!properties.ContainsKey("custom_username"))
                properties.Add("custom_username", tbxUsername.Text);
            else
                properties["custom_username"] = tbxUsername.Text;
            if (!properties.ContainsKey("custom_password"))
                properties.Add("custom_password", tbxPassword.Text);
            else
                properties["custom_password"] = tbxPassword.Text;
 
            properties.Update();
            lblMessages.Text = "Properties updated successfully!";
        }
        else
        {
            lblMessages.Text = "Please specify a Username and Password before pressing Save.";
        }
    }
    catch (Exception ex)
    {
        lblMessages.Text = ex.ToString();
    }
}

And there you go. Short, sweet, and hopefully useful.

The post Storing Credentials in a SharePoint Property Bag appeared first on MetroStar Systems Blog.


Viewing all articles
Browse latest Browse all 10

Trending Articles