Spaceman
Spaceman

Reputation: 1339

Getting full url not working when url contains #

Hello stack overflow peeps,

Having a bit of an issue implementing an oAuth system.

//https://github.com/justintv/Twitch-API/blob/master/authentication.md
public ActionResult AuthorizeTwitch(CancellationToken cancellationToken) {
    var twitchBridge = new TwitchBridge();
    var codes = Request.Params.GetValues("code");
    var token = Request.Params.GetValues("access_token");

    // stage 1 Initial Handshake
    if (codes == null && token == null) {
        return Redirect(twitchBridge.AuthorizeUrl());
    }
    // stage 2 We have a code so we are at stage two request the token
    else if (codes != null) {
        return Redirect(twitchBridge.RetrieveToken(codes[0]));
    }

    // SAVE OAUTH STUFF DOWN HERE.
    // THEN REDIRECT
    return RedirectToAction("Index", "Home");
}

The above action seems to work however when for stages 1 and 2 but when I get a response form stage 2 I get redirected to a URL that looks like.

http://localhost:58434/Home/AuthorizeTwitch#access_token=anaccesstoken&scope=user_read

This hits my action but I cant access the value for access_token. I can access Request.Url.OriginalString but the return string looks like.

http://localhost:58434/Home/AuthorizeTwitch

Debugging and looking into the request object and the URL object nothing seems to have stored anything from the # onwards. I suspect this has something to do with the routes setup for my site.

The relevant route that is being hit is

routes.MapRoute(
    name: "Default2",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Can any one see what i'm doing wrong?

In case it is something to do with my actually request to twitch this is how im building the url.

public string AuthorizeUrl() {
    return string.Format(
        "{0}{1}?response_type=code&client_id={2}&redirect_uri={3}&scope={4}",
        TwitchUrlBase,
        TwitchAuthorizeMethod,
        ClientId,
        HttpUtility.UrlEncode(TwitchAuthorizeRedirectURL),
        TwitchAuthorizeScope
    );
}

public string RetrieveToken(string code) {
    return string.Format(
        "{0}{1}?response_type=token&client_id={2}&client_secret={3}grant_type=authorization_code&redirect_uri={4}&scope={5}",
        TwitchUrlBase,
        TwitchAuthorizeMethod,
        ClientId,
        ClientSecret,
        HttpUtility.UrlEncode(TwitchAuthorizeRedirectURL),
        TwitchAuthorizeScope
    );
}

Upvotes: 1

Views: 289

Answers (1)

James
James

Reputation: 3928

The fragment is not supposed to be sent by the client to the server, so it makes sense that it's not there. Did you mean to send a query?

Upvotes: 1

Related Questions