void HandleLoginError(Exception e = null) { UpdateServiceStatus(); Context?.Dispose(); Context = new NiconicoContext(); LogInFailed?.Invoke(this, new NiconicoSessionLoginErrorEventArgs() { LoginFailedReason = InternetConnection.IsInternet() ? LoginFailedReason.ServiceNotAvailable : LoginFailedReason.OfflineNetwork , Exception = e, }); }
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; } })); }