/// <summary> /// ログインする /// </summary> /// <exception cref="InvalidOperationException">すでにログインしている</exception> /// <exception cref="LoginFailureNiconicoLoginSessionException"></exception> /// <exception cref="NetworkNiconicoLoginSessionException"></exception> public async Task Login() { if (!this.BadSession && this.IsLoggedin) { throw new InvalidOperationException("すでにログインしています"); } if (!this.BadSession && nicosid != null && nicosid.Length > 0 && session != null && session.Length > 0 && secure != null && secure.Length > 0) { this.cookie = new CookieCollection(); this.cookie.Add(new Cookie("nicosid", nicosid, "/", "nicovideo.jp")); this.cookie.Add(new Cookie("user_session", session, "/", "nicovideo.jp")); this.cookie.Add(new Cookie("user_session_secure", secure, "/", "nicovideo.jp")); return; } const string loginUrl = "https://secure.nicovideo.jp/secure/login?site=niconico"; var handler = new HttpClientHandler(); using var client = new HttpClient(handler); var content = new FormUrlEncodedContent(new Dictionary <string, string> { { "next_url", "" }, { "mail", this.mail }, { "password", this.password } }); try { await client.PostAsync(loginUrl, content).ConfigureAwait(false); } catch (HttpRequestException e) { throw new NetworkNiconicoLoginSessionException(e); } CookieCollection cookieCollection = handler.CookieContainer.GetCookies(new Uri(loginUrl)); if (cookieCollection.All(x => x.Name != "user_session")) { throw new LoginFailureNiconicoLoginSessionException(); } Cookie cookieNicosid = cookieCollection.Where(x => x.Name == "nicosid").Single(); Cookie cookieSession = cookieCollection.Where(x => x.Name == "user_session").Single(); Cookie cookieSecure = cookieCollection.Where(x => x.Name == "user_session_secure").Single(); this.nicosid = cookieNicosid.Value; this.session = cookieSession.Value; this.secure = cookieSecure.Value; this.BadSession = false; this.cookie = cookieCollection; }
/// <summary> /// ログインする /// </summary> /// <exception cref="InvalidOperationException">すでにログインしている</exception> /// <exception cref="LoginFailureNiconicoLoginSessionException"></exception> /// <exception cref="NetworkNiconicoLoginSessionException"></exception> public async Task Login() { if (this.IsLoggedin) { throw new InvalidOperationException("すでにログインしています"); } const string loginUrl = "https://secure.nicovideo.jp/secure/login?site=niconico"; var handler = new HttpClientHandler(); using var client = new HttpClient(handler); var content = new FormUrlEncodedContent(new Dictionary <string, string> { { "next_url", "" }, { "mail", this.mail }, { "password", this.password } }); try { await client.PostAsync(loginUrl, content).ConfigureAwait(false); } catch (HttpRequestException e) { throw new NetworkNiconicoLoginSessionException(e); } CookieCollection cookieCollection = handler.CookieContainer.GetCookies(new Uri(loginUrl)); if (cookieCollection.All(x => x.Name != "user_session")) { throw new LoginFailureNiconicoLoginSessionException(); } this.cookie = cookieCollection; }