@page "{code}" @model yawaflua.ru.Pages.AuthorizeModel @using System.Text.Json.Nodes @using Newtonsoft.Json @{ string path = $"{Request.Method}//{Request.Host}"; ViewData["Title"] = "Authorize"; string authorizationUrl = $"https://discord.com/api/oauth2/authorize?client_id={Startup.clientId}&response_type=code&redirect_uri={Startup.redirectUrl}&scope=identify";

Data: @Startup.clientId @Startup.redirectUrl [@String.Join(",", Startup.ownerId)] @Model.code

if (Model.code == null) { if (Request.Cookies["melon"]?.ToString() == null) { Login while Discord } else { HttpResponseMessage message; using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://discordapp.com/api/oauth2/@me")) { requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Request.Cookies["melon"]); ; message = await Startup.client.SendAsync(requestMessage); } string responseBody = await message.Content.ReadAsStringAsync(); JsonNode response = JsonNode.Parse(responseBody); if (response["user"] != null) {

Вы авторизованы!

Админка } else {

Токен авторизации неправильный! Попробуйте заново

} } } else { HttpResponseMessage message; using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://discordapp.com/api/oauth2/token")) { requestMessage.Content = new StringContent( @$"grant_type=authorization_code&code={Model.code}&client_id={Startup.clientId}&client_secret={Startup.clientSecret}&scope=identify&redirect_uri={Startup.redirectUrl}", new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded") ); message = await Startup.client.SendAsync(requestMessage); } string responseBody = await message.Content.ReadAsStringAsync(); JsonNode? body = JsonNode.Parse(responseBody); if (body["access_token"]?.ToString() == null) {

Ошибка! Попробуй авторизоваться заново

Console.Error.WriteLine("debug: START \\/ \ninfo: Don't worry, this message is not bad as you think"); Console.Error.WriteLine("error: DiscordAuthorize is not worked"); Console.Error.WriteLine($"debug: Body from discord: {body}\ndebug: Sended data to discord: {message.Content.ReadAsStringAsync().Result}"); Console.Error.WriteLine($"info: Check environment data: \n ClientId={Startup.clientId}\n ClientSecret={Startup.clientSecret}\n RedirectUrl={Startup.redirectUrl}\n OwnerId={String.Join(",", Startup.ownerId)} "); Console.Error.WriteLine("info: If any data is null and you set data in environment or appsettings.json, please create issue with this debug messages "); Console.Error.WriteLine("debug: END /\\"); } else { Response.Cookies.Append("melon", body["access_token"].ToString()); Response.Cookies.Append("watermelon", body["refresh_token"].ToString()); try { HttpContent bodytoApi; bodytoApi = new StringContent(JsonConvert.SerializeObject(new yawaflua.ru.Controllers.ApiController.authorizeBody() { discordId = body["user"]["id"].ToString(), melon = body["access_token"].ToString(), type = Auth.ApiKeyTypes.Public, watermelon = body["watermelon"].ToString() })); var req = await Startup.client.PostAsync(path + "/api/authorize", bodytoApi); } catch(Exception ex) { Console.WriteLine(body); Console.WriteLine(ex.Message); } Response.Redirect("/authorize", true); } } }