0

I am designing a website in ASP.NET, and I am integrating the Steam API into it. I am using DotNetOpenAuth for the authentication. I am confused as to how I am to access and display the variable responseURI (Line 22). Whenever I try to display the variable on the webpage using this:

<p><%=responseURI%></p>

I get a message stating:

The name 'responseURI' does not exist in the current context.

Here is my C#

using DotNetOpenAuth.OpenId.RelyingParty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class loginPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var openid = new OpenIdRelyingParty();
        var response = openid.GetResponse();

        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    // do success
                    var responseURI = response.ClaimedIdentifier.ToString();
                    break;

                case AuthenticationStatus.Canceled:
                case AuthenticationStatus.Failed:
                    // do fail
                    break;
            }
        }
        else
        {
            using (OpenIdRelyingParty openidd = new OpenIdRelyingParty())
            {
                IAuthenticationRequest request = openidd.CreateRequest("http://steamcommunity.com/openid");
                request.RedirectToProvider();
            }
        }
    }
}
SlothGod
  • 356
  • 1
  • 4
  • 19
  • 1
    Possible duplicate of [how to call a variable in code behind to aspx page](https://stackoverflow.com/questions/7406961/how-to-call-a-variable-in-code-behind-to-aspx-page) – NineBerry Oct 15 '17 at 11:15

1 Answers1

4

Because it doesn't exist in the page's context, only in the context of that switch case. The Page is the class, just make it a class-level value. (I believe the visibility needs to be at least protected, since the displayed page "inherits" from the code-behind page.)

protected string ResponseURI { get; set; }

That would be placed at the class-level, like any other property in C#. For example:

protected string ResponseURI { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    //...
}

Then set that in your code instead:

this.ResponseURI = response.ClaimedIdentifier.ToString();

And display it on the page:

<%=ResponseURI%>
David
  • 208,112
  • 36
  • 198
  • 279
  • @SlothGod: In your code-behind, of course. The property is a class-level member, and the other two lines replace your current attempted usages of your local `responseURI` variable. – David Oct 15 '17 at 11:18
  • I mean where in my code behind? What line? Sorry if I am seeming rude at all, I don't mean to, I just read back my comments and it looks worse than I meant to say it – SlothGod Oct 15 '17 at 11:22
  • @SlothGod: Well, since this is replacing the `responseURI` variable, you'd generally start on the line which declares and uses the `responseURI` variable. – David Oct 15 '17 at 11:24
  • Now I'm getting the same error message as before, but now for a lot of other variables. – SlothGod Oct 15 '17 at 11:27
  • @SlothGod: Then it would appear that you've made a mistake. – David Oct 15 '17 at 11:28
  • Just so that I am getting this. I am replacing the line `var responseURI = response.ClaimedIdentifier.ToString();` with `this.ResponseURI = response.ClaimedIdentifier.ToString();` and placing `protected string ResponseURI { get; set; }` before it? If not, could you possibly edit your answer and insert the code where it is meant to be placed? – SlothGod Oct 15 '17 at 11:31
  • @SlothGod: You're placing the declaration of the property *at the class level*, like any other property. – David Oct 15 '17 at 11:32
  • Still not following. I am familiar with a reasonable amount of C#, just not at the advanced level of it . Would you mind editing your answer to include the code where it is meant to be placed? – SlothGod Oct 15 '17 at 11:33
  • 2
    @SlothGod: Answer updated. Not to sound condescending or anything, but if you're not familiar with the basic structure of C# code then what you're trying to do is a bit advanced for you. You should really be started with some introductory tutorials first. – David Oct 15 '17 at 11:35