[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 895
  • Last Modified:

How to encrypt a password on FormView update and insert event using an EntityDataSource

I have a FormView with a textbox for a password.  The FormView is using an EntityDataSource as a data source an I am trying to apply a hash function the textbox value before it is entered into the database.

Formview:

<asp:FormView
      ID="fvUser"
      runat="server"
      DataKeyNames="id"
      DataSourceID="edsUserDetails"
      DefaultMode="ReadOnly"
      OnItemInserting="fvUser_ItemInserting"
      width="100%"
      >
        <EditItemTemplate>
              .
              .
              .
            <div class="label-Field">Password</div>
                <asp:TextBox ID="tbxPassword" runat="server" Width="200px" TextMode="Password" Text='<%# Bind("password") %>' />
         </EditItemTemplate>
</asp:FormView>

Entity Datasource:

<asp:EntityDataSource ID="edsUserDetails" runat="server"
                        ConnectionString="name=WomEntities"
                        DefaultContainerName="WomEntities"                         
                        EntitySetName="Users"
                        EnableUpdate="True"
                        EnableInsert="True"
                        OnInserted="edsUserDetails_ItemInserted"
                        OnUpdating="edsUserDetails_onUpdating"
                        AutoGenerateWhereClause="true">
                        <WhereParameters>
                              <asp:ControlParameter ControlID="fView$gvSearchResult" DbType="Int32" Name="id" PropertyName="SelectedValue" />
                        </WhereParameters>
                  </asp:EntityDataSource>

Code Behind:

protected void edsUserDetails_onUpdating( object sender, EntityDataSourceChangingEventArgs e ) {
                  TextBox ltb_password = ( TextBox )fvUser.FindControl( "tbxPassword" );
                  string lpswd = encrypt( ltb_password.Text );
                  e.Entity.password = lpswd;                  
            }

I found a blog that suggested the onUpdating method above but it is returning error:  "Error      3      'object' does not contain a definition for 'password' and no extension method 'password' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"

Any suggestions for this.  I simply want to use the existing code and interrupt the database insert/update to hash the password.

Thank you
0
meyerc74
Asked:
meyerc74
1 Solution
 
NazoUKCommented:
You'll need to cast the E.Entity in the OnUpdating function to whatever your entity type is to be able to set its properties.
0
 
meyerc74Author Commented:
thank you NazoUK....i knew it was something simple i was overlooking.  Corrected code below:

protected void edsUserDetails_onUpdating( object sender, EntityDataSourceChangingEventArgs e ) {
                  TextBox ltb_password = ( TextBox )fvUser.FindControl( "tbxPassword" );
                  string lpswd = encrypt( ltb_password.Text );
                  User lentity = (User)e.Entity;
                  lentity.password = lpswd;
            }
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now