Monday, January 26, 2015

Change Password of a (local) Windows User Account

In this post I want to show, how one can change the password of a Windows user using C#.
For this there are multiple possibilities, here we use the class PrincipalContext.
Using this, but also the other methods, in Windows 8 it is important that the type of user account is set to local (can be changed), and not to Windows account.
In the project we add a reference to the assembly System.DirectoryServices.AccountManagement, and with the following short code the password of a user can be changed:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.DirectoryServices.AccountManagement;

namespace ChangePW
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
                var Context = new PrincipalContext(ContextType.Machine);
                var User = UserPrincipal.FindByIdentity(Context, IdentityType.SamAccountName, username);
                User.ChangePassword(oldPassword, newPassword);
        }
    }
}

As one can see, we first create a PrincipalContext object. ContextType.Machine determines, that we want to access the local computer. From this we then select the desired user, and change its password.

Thursday, January 15, 2015

Traverse Posts on Blogger

After I showed in the previous post how to publish new posts on Blogger using C# and the Blogger Data API v2, I want to show today how one can scan through the already existing posts on one's blog.
We do the same preliminary work like in the previous post, meaning we include Google.GData.Client and create the Service object, with which we log on.
With the following feed query we then can access all published posts:

            FeedQuery query = new FeedQuery();
            query.Uri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");

            AtomFeed feed = null;
            feed = service.Query(query);

The following example program searches through the contents of all existing posts for the target expression "Test" and then returns the title of all matching posts:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Google.GData.Client;

namespace BloggerQueryPosts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Service service = new Service("blogger""");
            service.Credentials = new GDataCredentials(Email, Password);
            GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
            factory.AccountType = "GOOGLE";

            FeedQuery query = new FeedQuery();
            query.Uri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");

            AtomFeed feed = null;
            feed = service.Query(query);
            foreach (AtomEntry entry in feed.Entries)
            {
                if (entry.Content.Content.ToString().Contains("Test"))
                    MessageBox.Show("Match. " + entry.Title.Text);
            }
        }
    }
}

Sunday, January 11, 2015

Publish Posts on Blogger.com

The next posts will be about the blogging portal Blogger.com, on which for example this blog is running. For the access via C# we need the Blogger Data API, as is described in this documentation. Here we use version 2, but there is already a (beta) version 3, which I will probably describe later.
In today's post I want to show, how to create and publish posts with C#.
For this we first have to download the Google_Data_API_Setup from this link and install it. Then we add a reference to Google.GData.Client.dll to our project, which is in my case located in the folder C:\Program Files (x86)\Google\Google Data API SDK\Redist, and integrate it to our project via using Google.GData.Client; 
Now we can log on to Blogger:

            Service service = new Service("blogger", "");
            service.Credentials = new GDataCredentials(Email, Password);
            GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
            factory.AccountType = "GOOGLE";

Posts are created in the atom format, with these lines of code we create posts with title "Test" and content "Testpost":

            AtomEntry newPost = new AtomEntry();
            newPost.Title.Text = "Test";
            newPost.Content = new AtomContent();
            newPost.Content.Content = "Testpost";
            newPost.Content.Type = "xhtml";
            // newPost.IsDraft = true;

The attributes should be self explaining, via IsDraft we set whether the post should be a draft.
Finally we have to publish the post, for this we send the post to the blog feed, which is done like this:


            Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");
            AtomEntry createdEntry = service.Insert(blogFeedUri, newPost);

The complete code looks like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Google.GData.Client;

namespace BloggerCreatePosts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Service service = new Service("blogger", "");
            service.Credentials = new GDataCredentials(Email, Password);
            GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
            factory.AccountType = "GOOGLE";

            AtomEntry newPost = new AtomEntry();
            newPost.Title.Text = "Test";
            newPost.Content = new AtomContent();
            newPost.Content.Content = "Testpost";
            newPost.Content.Type = "xhtml";
            // newPost.IsDraft = true;


            Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");
            AtomEntry createdEntry = service.Insert(blogFeedUri, newPost);
        }
    }
}