<?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 : MembershipProvider</title><link>http://blogs.iis.net/rakkimk/archive/tags/MembershipProvider/default.aspx</link><description>Tags: MembershipProvider</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><item><title>ASP.NET - Enabling PasswordReset functionality when using ActiveDirectoryMembershipProvider</title><link>http://blogs.iis.net/rakkimk/archive/2008/04/11/enabling-passwordreset-functionality-when-using-activedirectorymembershipprovider.aspx</link><pubDate>Fri, 11 Apr 2008 22:59:00 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2293215</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=2293215</wfw:commentRss><comments>http://blogs.iis.net/rakkimk/archive/2008/04/11/enabling-passwordreset-functionality-when-using-activedirectorymembershipprovider.aspx#comments</comments><description>&lt;P&gt;If you want to use ActiveDirectoryMembershipProvider on your website to manage users specially the password reset functionality, you will also need to create few attributes in the active directory schema for the "USER" object. You can check &lt;A href="http://msdn2.microsoft.com/en-us/library/ms998360.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms998360.aspx"&gt;this MSDN article&lt;/A&gt; to know more about this, but again, it doesn't list how to create the needed attributes, but it tells you what are all the attributes needed if you are considering "Password Reset" functionality. &lt;/P&gt;
&lt;P&gt;Firstly, ActiveDirectoryMembershipProvider does not support retrieving the password, but you can reset the password by providing secret-question, and secret-answer. You may also need to create few more attributes in the active directory schema associated with this. Below are those attributes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Password Question - &lt;STRONG&gt;Unicode String&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Password Answer - &lt;STRONG&gt;Unicode String&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Failed Answer count - &lt;STRONG&gt;Integer&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Last time at which the user supplied an invalid answer - &lt;STRONG&gt;Large Integer/Interval&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Account locked out time - &lt;STRONG&gt;Large Integer/Interval&lt;/STRONG&gt; &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;These are the 5 new attributes which you need to add in the active directory schema for the "USER" object. I will explain how to add new attributes and associate them to an existing object. &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You need to first install the schema snap-in by registering schmmgmt.dll (&lt;STRONG&gt;regsvr32 schmmgmt.dll&lt;/STRONG&gt;) &lt;/LI&gt;
&lt;LI&gt;Now, open an MMC, and &lt;STRONG&gt;add "Active Directory Schema" snap-in&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Expand the Active Directory Schema, and right click on Attribute, and select &lt;STRONG&gt;"Create Attribute"&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Enter the common name, LDAP name, other fields for the attribute you are creating. For example, "PasswordQuestion" - this would be having its type as &lt;STRONG&gt;Unicode String. &lt;/STRONG&gt;See the above list of attributes and its types appropriately. If Integer, enter minimum/maximum values too. &lt;/LI&gt;
&lt;LI&gt;For the OID, you need to check &lt;A href="http://msdn2.microsoft.com/en-us/library/ms677620.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms677620.aspx"&gt;this MSDN article&lt;/A&gt;. &lt;/LI&gt;&lt;/OL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;A href="http://blogs.iis.net/blogs/rakkimk/WindowsLiveWriter/a9408e504f9b_13E3B/image_2.png" mce_href="http://blogs.iis.net/blogs/rakkimk/WindowsLiveWriter/a9408e504f9b_13E3B/image_2.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=391 alt=image src="http://blogs.iis.net/blogs/rakkimk/WindowsLiveWriter/a9408e504f9b_13E3B/image_thumb.png" width=390 border=0 mce_src="http://blogs.iis.net/blogs/rakkimk/WindowsLiveWriter/a9408e504f9b_13E3B/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now follow the above steps to create all the 5 attributes which are needed. After creating these attributes, we need to attach them to the "USER" object. &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;In the same MMC, Expand &lt;STRONG&gt;"CLASSES" &lt;/STRONG&gt;and select &lt;STRONG&gt;user &lt;/STRONG&gt;object. &lt;/LI&gt;
&lt;LI&gt;Right click on &lt;STRONG&gt;user&lt;/STRONG&gt; and select properties &lt;/LI&gt;
&lt;LI&gt;Go to its attributes tab, and click &lt;STRONG&gt;Add&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Select the attributes that you've created one by one and click on OK &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;That's it. Now, your user object would have all those attributes, and you can store values using any method you like. If you create an user using CreateUser wizard control, it would populate and store the values of the secret-question, answer automatically. ActiveDirectoryMembershipProvider would take care of storing, retrieving values of these attributes itself, you no need to program anything for them. &lt;/P&gt;
&lt;P&gt;But, there would be some situation the users have been already created, but you need to attach these attributes to them. Follow the above methods to add attributes to the &lt;STRONG&gt;user &lt;/STRONG&gt;object. And, now open the particular user's properties in ADSIEDIT.msc, and add values to them. &lt;/P&gt;
&lt;P&gt;After following all the above steps, follow the other steps mentioned in &lt;A href="http://msdn2.microsoft.com/en-us/library/ms998360.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms998360.aspx"&gt;this article&lt;/A&gt; to configure your web.config sections to map the attributes you've created in AD. &lt;/P&gt;
&lt;P&gt;NOTE: Password-answer is the only one attribute out of these 5 which would be stored in an encrypted format. &amp;lt;machinekey&amp;gt; section would be used for the encryption of this, if you create an user using the CreateUser wizard. But, if you have already created the user in the AD, and you want to just store the secret-question and password, you may want to check my next blog where I'll explain how to use the same encryption method used by the MembershipProvider to store the secret-password in the active directory for the user. &lt;/P&gt;&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2293215" 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></item></channel></rss>