apidotnetopenauthsteam

Steam Login Authentication C#


Im trying to make steam login work for my website with DotNetOpenAuth.

But looking through the examples in the documentation doesn't give me any idea how to make it work.

Here´s what I done so far:

1) Added the dotnetopenauth reference files to \bin and to the configuration

2) Added a unique user field in the database for the response i get back from DotNetOpenAuth.

So heres my question

How can i retrive the steam id with DotNetOpenAuth?

I found some examples done in php: http://forums.steampowered.com/forums/showthread.php?t=1430511


Solution

  • This should do it, when you include the correct DotNetOpenAuth.

    This is code you would typically put in your Login page, the first if checks whether we have a response from Steam and deals with the response.

    The else part makes sets up the request and redirects the user to Steam - steam will then redirect back to this page once the user has logged in on steam.

    Unlike other open auth providers, steam does not provide other user information (email, etc...) by sending a claims request with the request - it will only provide a URL in the response.ClaimedIdentifier which is a URL containing the users steam id at the end.

    You will have to do the string manipulation to only get the ID if you want.

    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();
                    //"http://steamcommunity.com/openid/id/76561197969877387"
                    // last part is steam user id
                    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();
            }
        }
    }