<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.iis.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:cs="http://blogs.iis.net/"><channel><title>rakkimk : ActiveDirectoryMembershipProvider</title><link>http://blogs.iis.net/rakkimk/archive/tags/ActiveDirectoryMembershipProvider/default.aspx</link><description>Tags: ActiveDirectoryMembershipProvider</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>ASP.NET - Using the same encryption method used by ActiveDirectoryMembershipProvider to encrypt secret password answer and store it in AD</title><link>http://blogs.iis.net/rakkimk/archive/2008/04/11/asp-net-using-the-same-encryption-method-used-by-activedirectorymembershipprovider-to-encrypt-secret-password-answer-and-store-it-in-ad.aspx</link><pubDate>Sat, 12 Apr 2008 00:26:29 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2293285</guid><dc:creator>rakkimk</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.iis.net/rakkimk/rsscomments.aspx?PostID=2293285</wfw:commentRss><comments>http://blogs.iis.net/rakkimk/archive/2008/04/11/asp-net-using-the-same-encryption-method-used-by-activedirectorymembershipprovider-to-encrypt-secret-password-answer-and-store-it-in-ad.aspx#comments</comments><description>&lt;p&gt;Okay, this is an interesting stuff. MembershipProvider automatically encrypts most of the sensitive information such as password, secret-question-password. What if you want to use the same encryption method yourself to encrypt data?  &lt;p&gt;Before continuing reading, You need to understand and keep in mind that your &amp;lt;machinekey&amp;gt; section is the one which would be used for the encryption / decryption by the MembershipProvider. &lt;strong&gt;If you change it after encryption, your decryption may fail.&lt;/strong&gt; So, please be careful while modifying anything on &amp;lt;machinekey&amp;gt; section in your web.config.  &lt;p&gt;I've just created a class inheriting from MembershipProvider. I've implemented all the methods of it (just a dummy implementation - VS would be more than happy to do that for you - if you find difficulty in this, write to me; I'll help you). I've also created another new method called EncryptMe which takes a string and returns me a string which is in fact the encrypted string. This method just gets the string in bytes with RNGCryptoServiceProvider and just call the function EncryptPassword of the MembershipProvider class to do the encryption.  &lt;p&gt;In fact, the EncryptPassword method is a protected method of the MembershipProvider class, and by using it, we have just achieved the same encryption which is used by the MembershipProvider class (which our ActiveDirectoryMembershipProvider also uses to encrypt your secret-password-answer). Since it is protected, you can't access it anywhere outside, but inside a derived class.  &lt;p&gt; &lt;div style="padding-right: 0px; padding-left: 0px; background: #808080; padding-bottom: 0px; margin-left: 4px; margin-right: 4px; padding-top: 0px"&gt; &lt;div style="border-right: #2e595c 1px solid; border-top: #2e595c 1px solid; background: #fff; left: -2px; border-left: #2e595c 1px solid; color: black; border-bottom: #2e595c 1px solid; position: relative; top: -2px"&gt; &lt;div style="border-right: blue 1px solid; border-top: blue 1px solid; background: #3f73b6; border-left: blue 1px solid; width: 100%; color: white; border-bottom: blue 1px solid"&gt;&lt;span style="width: 100%"&gt;Source of my EncryptMe Function&lt;/span&gt;&lt;/div&gt;&lt;pre&gt;    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; EncryptMe(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; s)
    {
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;byte&lt;/span&gt;&lt;span style="color: #000000"&gt;[] bytes = System.Text.&lt;/span&gt;&lt;span style="color: #00ff00"&gt;Encoding&lt;/span&gt;&lt;span style="color: #000000"&gt;.Unicode.GetBytes(s);
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;byte&lt;/span&gt;&lt;span style="color: #000000"&gt;[] data = &lt;/span&gt;&lt;span style="color: #2b91af"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;byte&lt;/span&gt;&lt;span style="color: #000000"&gt;[0x10];
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Security.Cryptography.&lt;/span&gt;&lt;span style="color: #00ff00"&gt;RNGCryptoServiceProvider&lt;/span&gt;&lt;span style="color: #000000"&gt;().GetBytes(data);
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;byte&lt;/span&gt;&lt;span style="color: #000000"&gt;[] dst = &lt;/span&gt;&lt;span style="color: #2b91af"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;byte&lt;/span&gt;&lt;span style="color: #000000"&gt;[data.Length + bytes.Length];
        &lt;/span&gt;&lt;span style="color: #00ff00"&gt;Buffer&lt;/span&gt;&lt;span style="color: #000000"&gt;.BlockCopy(data, 0, dst, 0, data.Length);
        &lt;/span&gt;&lt;span style="color: #00ff00"&gt;Buffer&lt;/span&gt;&lt;span style="color: #000000"&gt;.BlockCopy(bytes, 0, dst, data.Length, bytes.Length);
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;byte&lt;/span&gt;&lt;span style="color: #000000"&gt;[] b = EncryptPassword(dst);
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #00ff00"&gt;Convert&lt;/span&gt;&lt;span style="color: #000000"&gt;.ToBase64String(b);
    }&lt;/PRE&lt; DIV&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now, you can just store the encrypted string to the active directory property which you've mapped to the Secret-question-password. Check &lt;a href="http://support.microsoft.com/kb/555205"&gt;this knowledge base article&lt;/a&gt; which explains how to modify an attribute of an user in active directory. It just talks about the properties needed by the FTP user isolation, just modify the code to use your own attribute. &lt;/p&gt;
&lt;p&gt;Again, please make sure you do not alter your &amp;lt;machinekey&amp;gt; section which has all the information needed to encrypt and decrypt data. &lt;/p&gt;
&lt;p&gt;Hope this helps!&lt;/p&gt;&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2293285" width="1" height="1"&gt;</description><category domain="http://blogs.iis.net/rakkimk/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.iis.net/rakkimk/archive/tags/MembershipProvider/default.aspx">MembershipProvider</category><category domain="http://blogs.iis.net/rakkimk/archive/tags/ActiveDirectoryMembershipProvider/default.aspx">ActiveDirectoryMembershipProvider</category></item></channel></rss>