0

I am trying to pass values via query string to another page but it sends them empty. Why ?

 <a href='UpcomingNotifications.aspx?OrgID=<%# SharedUtility.EncryptURL("10") %>&RoleID=<%# SharedUtility.EncryptURL("1") %>'
    target="_blank" 
    class="pull-right" 
    style="margin-top: -44px; margin-right: 6px;">
     <asp:Label ID="lblUpcomingWorks" runat="server" 
          CssClass="btn btn-sm- btn-danger" 
          Text=" Upcoming Works Openings" Visible="false" />
</a>

Output: http://localhost:5297/forms/admin/UpcomingNotifications.aspx?OrgID=&RoleID=

Update: Encryption method:

public static String EncryptURL(string strData)
    {
        try
        {
            if (!String.IsNullOrEmpty(strData))
            {
                SHA1Managed shaM = new SHA1Managed();
                Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(strData)));
                Byte[] encByteData;
                encByteData = ASCIIEncoding.ASCII.GetBytes(strData);
                String encStrData = Convert.ToBase64String(encByteData);
                return encStrData;
            }
            else
            {
                return "";
            }
        }
        catch (Exception) { return ""; }

    }
Covert
  • 480
  • 1
  • 7
  • 25
  • Which kind of value returned by `SharedUtility.EncryptURL()` method? Try using `<%= SharedUtility.EncryptURL("n") %>` to render literal string. – Tetsuya Yamamoto Mar 01 '17 at 07:48
  • it returns string data but in my case it returns empty string – Covert Mar 01 '17 at 07:51
  • and i tried what you said but still empty – Covert Mar 01 '17 at 07:55
  • Is `SharedUtility.EncryptURL()` works fine in another occasions or returns proper value? Better to show the method code here. The `<%# ... %>` directive often used for data-binding with databound controls. – Tetsuya Yamamoto Mar 01 '17 at 07:55
  • yes I am binding controls but for simplicity I have written 10 and 1 which are also getting passed as empty strings. – Covert Mar 01 '17 at 08:00
  • actual code is : – Covert Mar 01 '17 at 08:00
  • Assume your method was `public string EncryptURL(string number)`, which kind of string format do you want to return with, also where `LoginOrganizationID` and `LoginRoleID` comes from? The method should be returns literal string to bind with HTML hyperlink `href` attribute. – Tetsuya Yamamoto Mar 01 '17 at 08:04
  • @TetsuyaYamamoto check udpated question – Covert Mar 01 '17 at 09:08
  • 2 conditions returning empty string: when `String.IsNullOrEmpty` is true or throwing certain exception (probably related to `Convert.ToBase64String` method). Put breakpoint on both else and catch block returns, see which one conditions apply: else block has reached or `Exception` returns error message. – Tetsuya Yamamoto Mar 01 '17 at 09:21

2 Answers2

1

SharedUtility.EncryptURL("10") and SharedUtility.EncryptURL("10") returns empty strings which are written, this is why you have no value there. You need to fix the problems in that method. If you edit your question with information about the method, then please leave a comment here so I can edit my answer. The parameter looks like a key, you might be using the wrong key.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • You have an exception in the method, therefore the catch block is executed and an empty string is returned. Can you debug your code and tell me what is the exception and on which line? – Lajos Arpad Mar 01 '17 at 09:24
  • Well, I suspected that `Convert.ToBase64String` throwing an exception which silently absorbed by returning empty string. Put `Exception.Message.ToString()` to find out which exception being thrown. – Tetsuya Yamamoto Mar 01 '17 at 09:33
  • @TetsuyaYamamoto what did it tell you? Can you edit your question with the message and stack trace of the exception? You can reach the latter via the StackTrace property of an Exception object. – Lajos Arpad Mar 01 '17 at 09:55
  • @LajosArpad OP should edit his question to provide error details and stack trace output using `Exception.Message` & `Exception.StackTrace` property. Assumed both `LoginOrganizationID` and `LoginRoleID` converted properly to string values, the method should return Base64 encoded string in try block if no exception being thrown. – Tetsuya Yamamoto Mar 01 '17 at 10:00
  • @TetsuyaYamamoto sorry, I thought you were the one asking the question. My bad. – Lajos Arpad Mar 01 '17 at 10:14
0

The implementation for SHA1 hashing algorithm with Base64 string conversion should looks like this:

public static String EncryptURL(string strData)
{
    try
    {
        if (!String.IsNullOrEmpty(strData))
        {
            using (SHA1Managed shaM = new SHA1Managed())
            {
                // ASCIIEncoding.ASCII.GetBytes should return same byte array in this case
                byte[] encbytedata = Encoding.ASCII.GetBytes(strData);
                byte[] hash = shaM.ComputeHash(encbytedata);
                String encStrData = Convert.ToBase64String(hash);
                return encStrData;
            }
        }
        else
        {
            return "";
        }
    }
    catch (Exception) { ... }
}

In sample code given by OP, empty string will returned when one of these conditions below fulfilled:

  1. strData is null or empty string

  2. catch-block triggered by exception thrown in try-block

Since EncryptURL method used twice when doing binding to ASPX page using LoginOrganizationID and LoginRoleID, in this case possibly previous instance of SHA1Managed has not disposed properly or conversion error has occurred during creating hashed URL in try-block, hence it throwing exception which silently absorbed in catch-block by returning empty string into href attribute (will be edited based on further information from OP).

SHA1 hashing reference:

Hashing with SHA1 Algorithm in C#

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61