public async Task Relogin() { if (NiconicoContext != null) { var context = new NiconicoContext(NiconicoContext.AuthenticationToken); if (await context.SignInAsync() == NiconicoSignInStatus.Success) { NiconicoContext = context; } } }
public IAsyncOperation <NiconicoSignInStatus> SignIn(string mailOrTelephone, string password) { return(AsyncInfo.Run <NiconicoSignInStatus>(async(cancelToken) => { if (!Util.InternetConnection.IsInternet()) { NiconicoContext?.Dispose(); NiconicoContext = new NiconicoContext(); return NiconicoSignInStatus.Failed; } if (NiconicoContext != null && NiconicoContext.AuthenticationToken?.MailOrTelephone == mailOrTelephone && NiconicoContext.AuthenticationToken?.Password == password) { return NiconicoSignInStatus.Success; } await SignOut(); try { await _SigninLock.WaitAsync(); var context = new NiconicoContext(new NiconicoAuthenticationToken(mailOrTelephone, password)); context.AdditionalUserAgent = HohoemaUserAgent; LoginErrorText = ""; Debug.WriteLine("try login"); NiconicoSignInStatus result = NiconicoSignInStatus.Failed; try { result = await context.SignInAsync(); } catch { LoginErrorText = "サインインに失敗、再起動をお試しください"; } UpdateServiceStatus(result); NiconicoContext = context; if (result == NiconicoSignInStatus.Success) { Debug.WriteLine("login success"); using (var loginActivityLogger = LoggingChannel.StartActivity("login process")) { loginActivityLogger.LogEvent("begin login process."); var fields = new LoggingFields(); await Task.Delay(500); try { loginActivityLogger.LogEvent("getting UserInfo."); var userInfo = await NiconicoContext.User.GetInfoAsync(); LoginUserId = userInfo.Id; IsPremiumUser = userInfo.IsPremium; { try { var user = await NiconicoContext.User.GetUserDetail(LoginUserId.ToString()); LoginUserName = user.Nickname; UserIconUrl = user.ThumbnailUri; OnPropertyChanged(nameof(LoginUserName)); OnPropertyChanged(nameof(UserIconUrl)); } catch (Exception ex) { throw new Exception("ユーザー名取得のフォールバック処理に失敗 + " + LoginUserId, ex); } } fields.AddString("user id", LoginUserId.ToString()); fields.AddString("user name", LoginUserName); fields.AddBoolean("is premium", IsPremiumUser); loginActivityLogger.LogEvent("[Success]:get UserInfo.", fields, LoggingLevel.Information); } catch (Exception ex) { LoginErrorText = $"ユーザー情報の取得に失敗しました。再起動をお試しください。({ex.Message})"; fields.AddString("mail", mailOrTelephone); loginActivityLogger.LogEvent(LoginErrorText, fields, LoggingLevel.Warning); NiconicoContext.Dispose(); NiconicoContext = new NiconicoContext(); return NiconicoSignInStatus.Failed; } fields.Clear(); Debug.WriteLine("user id is : " + LoginUserId); // 0.4.0以前のバージョンからのログインユーザー情報の移行処理 try { await MigrateLegacyUserSettings(LoginUserId.ToString()); } catch { LoginErrorText = "ユーザー設定の過去バージョンとの統合処理に失敗しました。"; return NiconicoSignInStatus.Failed; } try { Debug.WriteLine("initilize: fav"); loginActivityLogger.LogEvent("initialize user favorite"); FollowManager = await FollowManager.Create(this, LoginUserId); } catch { LoginErrorText = "お気に入り情報の取得に失敗しました。再起動をお試しください。"; Debug.WriteLine(LoginErrorText); loginActivityLogger.LogEvent(LoginErrorText, fields, LoggingLevel.Error); NiconicoContext.Dispose(); NiconicoContext = new NiconicoContext(); return NiconicoSignInStatus.Failed; } FollowManagerUpdater = BackgroundUpdater.RegistrationBackgroundUpdateScheduleHandler( FollowManager, "FollowManager", label: "フォロー" ); Debug.WriteLine("Login done."); loginActivityLogger.LogEvent("[Success]: Login done"); } // BG更新をスケジュール UpdateAllComponent(); // 動画のキャッシュフォルダの選択状態をチェック await(App.Current as App).CheckVideoCacheFolderState(); // サインイン完了 OnSignin?.Invoke(); // TODO: 途中だった動画のダウンロードを再開 // await MediaManager.StartBackgroundDownload(); // ニコニコサービスの裏で取得させたいので強制的に待ちを挟む await Task.Delay(1000); } else { Debug.WriteLine("login failed"); NiconicoContext?.Dispose(); NiconicoContext = null; } return result; } finally { _SigninLock.Release(); } })); }
public IAsyncOperation <NiconicoSignInStatus> SignIn(string mailOrTelephone, string password, bool withClearAuthenticationCache = false) { return(AsyncInfo.Run <NiconicoSignInStatus>(async(cancelToken) => { using (var releaser = await SigninLock.LockAsync()) { if (!Helpers.InternetConnection.IsInternet()) { Context?.Dispose(); Context = new NiconicoContext(); return NiconicoSignInStatus.Failed; } if (Context != null && Context.AuthenticationToken?.MailOrTelephone == mailOrTelephone && Context.AuthenticationToken?.Password == password) { return NiconicoSignInStatus.Success; } } cancelToken.ThrowIfCancellationRequested(); if (IsLoggedIn) { await SignOut(); } cancelToken.ThrowIfCancellationRequested(); using (_ = await SigninLock.LockAsync()) { Debug.WriteLine("try login"); var context = new NiconicoContext(new NiconicoAuthenticationToken(mailOrTelephone, password)); context.AdditionalUserAgent = HohoemaUserAgent; if (withClearAuthenticationCache) { context.ClearAuthenticationCache(); } NiconicoSignInStatus result = NiconicoSignInStatus.Failed; //if ((result = await context.GetIsSignedInAsync()) == NiconicoSignInStatus.ServiceUnavailable) //{ // return result; //} try { result = await context.SignInAsync(); UpdateServiceStatus(result); if (result == NiconicoSignInStatus.TwoFactorAuthRequired) { var deferral = new Deferral(async() => { using (_ = await SigninLock.LockAsync()) { try { result = await context.GetIsSignedInAsync(); UpdateServiceStatus(result); if (context != null) { IsLoggedIn = true; await LoginAfterResolveUserDetailAction(context); } } catch (Exception e) { HandleLoginError(e); } } }); RequireTwoFactorAuth.Invoke(this, new NiconicoSessionLoginRequireTwoFactorAuthEventArgs() { Deferral = deferral, TwoFactorAuthPageUri = context.LastRedirectHttpRequestMessage.RequestUri }); } else if (result == NiconicoSignInStatus.Success) { IsLoggedIn = true; await LoginAfterResolveUserDetailAction(context); } else { LogInFailed?.Invoke(this, new NiconicoSessionLoginErrorEventArgs() { LoginFailedReason = LoginFailedReason.InvalidMailOrPassword }); } } catch (Exception e) { HandleLoginError(e); } return result; } })); }
public async Task Initialize() { _loginContext = new NiconicoContext(new NiconicoAuthenticationToken("*****@*****.**", "36yMhd")); await _loginContext.SignInAsync(); }