示例#1
0
        public async Task <IActionResult> AuthCallback(string code, string nonce = null)
        {
            if (string.IsNullOrEmpty(Request.Cookies["State"]))
            {
                return(BadRequest("State cookie not set or expired. Maybe you took too long to authorize. Please try again."));
            }
            else if (Request.Cookies["State"] != Request.Query["State"])
            {
                return(BadRequest("State verification failed."));
            }

            object result = null;

            if (code.Length > 0)
            {
                using (HttpClient client = new HttpClient())
                {
                    var spotiCredentials = await _spotifyService.AuthRequest(code, client);

                    var userData = await _spotifyService.UserInfoRequest(client, spotiCredentials);

                    var token = await GenerateToken(userData, spotiCredentials);

                    result = new
                    {
                        Spotify         = spotiCredentials,
                        SpotifyUserData = userData,
                        Firebase        = token
                    };
                }
            }

            return(Json(result));
        }
示例#2
0
        public async Task <ActionResult <AuthenticationResponse> > AuthCallback(string code, string State = null)
        {
            this.log.LogInformation("Entered mobile callback");
            if (string.IsNullOrEmpty(Request.Cookies["State"]))
            {
                return(BadRequest("State cookie not set or expired. Maybe you took too long to authorize. Please try again."));
            }
            else if (Request.Cookies["State"] != Request.Query["State"])
            {
                return(BadRequest("State verification failed."));
            }

            AuthenticationResponse result = null;

            if (code.Length > 0)
            {
                using (HttpClient client = new HttpClient())
                {
                    log.LogInformation("Entering Spotify AuthResquest");
                    var spotiCredentials = await _spotifyService.AuthRequest(code, client, false);

                    if (spotiCredentials == null)
                    {
                        return(BadRequest("Spotify returned a 400"));
                    }

                    log.LogInformation($"Spotify credentials provided: {JsonConvert.SerializeObject(spotiCredentials)}");
                    var userData = await _spotifyService.UserInfoRequest(client, spotiCredentials);

                    log.LogInformation($"User data: name: {userData.Display_name} user: {userData.Id}");
                    var token = await this.userService.GenerateToken(userData, spotiCredentials);

                    result = new AuthenticationResponse
                    {
                        Spotify         = spotiCredentials,
                        SpotifyUserData = userData,
                        Firebase        = token
                    };

                    log.LogInformation($"Returned answer {JsonConvert.SerializeObject(result)}");
                }
            }

            return(Json(result));
        }