Draity
Draity

Reputation: 394

SteamAuth var from startup.auth to the view ASP.NET

I want to get the profile information from steam. so first I've fixed that I can login through steam, I used this tutorial: http://www.oauthforaspnet.com/providers/steam/

But now I want to get the steam profile id from the user that logged in so I can use the JSON from the steam API to get the information from the user.

https://steamcommunity.com/profiles/(this id)

I hope someone can help me, I've searched for hours now, and don't have any result.

var options = new SteamAuthenticationOptions {
ApplicationKey = "Your API Key",
Provider = new OpenIDAuthenticationProvider // Steam is based on OpenID
{
    OnAuthenticated = async context =>
    {
        // Retrieve the user's identity with info like username and steam id in Claims property
        var identity = context.Identity;
    }
}}; app.UseSteamAuthentication(options);

Upvotes: 1

Views: 1442

Answers (1)

Draity
Draity

Reputation: 394

A while ago we discovered the answer:

1.) insert your key from the tutorial here:

var options = new SteamAuthenticationOptions
{
    ApplicationKey = "Your API Key",
    Provider = new OpenIDAuthenticationProvider // Steam is based on OpenID
    {
        OnAuthenticated = async context =>
        {
            // Retrieve the user's identity with info like username and steam id in Claims property
            var identity = context.Identity;
        }
    }
};
app.UseSteamAuthentication(options);

2.) We discovered that steam is saving a users steam id in the database table called: 'AspNetUserLogins', the providerkey inside that table is an url made out of more pieces. For example:

http://steamcommunity.com/openid/id/here-users-steamid

We only need the users steamid, so we going to split that in step 3.

3.) Make a controller, for example: SteamController. Here we going to add a public string:

public string GetSteamID()
    {
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new Steam.Models.ApplicationDbContext()));
        var CurrentUser = manager.FindById(User.Identity.GetUserId());
        if (User.Identity.Name != "")
        {
            string url = CurrentUser.Logins.First().ProviderKey;
            ViewBag.steamid = url.Split('/')[5]; //here we going to split the providerkey so we get the right part
        }
        else
        {
            ViewBag.steamid = "";
        }

        return ViewBag.steamid;
    }
  1. ) now we can add some stuff, lets say we going to add profile information. Go to your SteamController and add:

        [HttpGet]
    public ContentResult GetProfile()
    {
        string url = string.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=addyourkeyhere&steamids={0}", this.GetSteamID());
        string result = null;
    
        using (var client = new WebClient())
        {
            result = client.DownloadString(url);
        }
        return Content(result, "application/json");
    }
    

notice that you have to add your steam key from step 1 in the url.

  1. ) make a script called: profile.js. Here we going to add our profile information.

function profilepic() {
    $.ajax({
        url: 'http://localhost:3365/steam/GetProfile',
        dataType: 'json',
        success: function (data) {
            $.each(data.response.players, function (key, value) {
                if ($('.profile')) {
                    $('.profile').append("<img src='" + value.avatar + "'> <span>" + value.personaname + "</span>")
                }
                if ($('.profile1')) {
                    $('.profile1').append("<img src='" + value.avatarfull + "'>")
                }
                if ($('.username')) {
                    $('.username').append(value.personaname)
                }
                
                console.log(value)
            });
        }, error: function (httpReq, status, exception) {
            console.log(status + " " + exception);
        }
    });
}

6.) Now we have to do the final step, create a view with the classes, for example:

<div class="userprofile">
    <span class="profile1"></span>
    <div class="userdescription">
        <h2 class="username"></h2>
    </div>
</div>
  1. ) I hope this will help some people, for more questions, feel free to ask!

Upvotes: 1

Related Questions