コード例 #1
0
        public async Task <IActionResult> SigninCallbackAsync(string userData = null, string remoteError = null)
        {
            try {
                if (string.IsNullOrWhiteSpace(userData))
                {
                    Log.Error("userData is not defined");
                    return(BadRequest(_localizer[DataTransferer.DefectiveEntry().Message]));
                }

                var headerbindingmodel = JsonConvert.DeserializeObject <Device>(Encoding.UTF8.GetString(Convert.FromBase64String(userData)));
                if (headerbindingmodel == null ||
                    string.IsNullOrWhiteSpace(headerbindingmodel.DeviceId) ||
                    string.IsNullOrWhiteSpace(headerbindingmodel.DeviceName) ||
                    string.IsNullOrWhiteSpace(headerbindingmodel.DeviceType))
                {
                    Log.Error("userData is not valid");
                    return(BadRequest(_localizer[DataTransferer.DefectiveEntry().Message]));
                }

                // read external identity from the temporary cookie
                var authenticationResult = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme).ConfigureAwait(true);

                if (!authenticationResult.Succeeded)
                {
                    Log.Error("External authentication failed");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationFailed().Message]));
                }

                // retrieve claims of the external user
                var claimPrincipal = authenticationResult.Principal;
                if (claimPrincipal == null)
                {
                    Log.Error("External authentication user principal error");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationUserError().Message]));
                }

                // transform claims list to model
                var externalUser = GetExternalUser(claimPrincipal.Claims);

                if (string.IsNullOrWhiteSpace(externalUser.Email))
                {
                    Log.Error("External authentication user email not found");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationEmailError().Message]));
                }

                if (externalUser.ProviderId == AccountProvider.Clipboardy)
                {
                    Log.Error("External signup with unknown ProviderId");
                    return(Problem(_localizer[DataTransferer.ExternalAuthenticationWithUnknownProvider().Message]));
                }

                externalUser.DeviceId   = headerbindingmodel.DeviceId;
                externalUser.DeviceName = headerbindingmodel.DeviceName;
                externalUser.DeviceType = headerbindingmodel.DeviceType;

                var accountprofile = await _accountProfileService.FirstAsync(new AccountProfileGetFirstSchema {
                    TypeId   = AccountProfileType.Email.ToInt(),
                    LinkedId = externalUser.Email
                }).ConfigureAwait(true);

                if (accountprofile == null)
                {
                    Log.Debug($"User {User.Identity.Name} try to sign in for first time at {DateTime.UtcNow}");
                    return(Ok(_accountService.ExternalSignupAsync(externalUser)));
                }
                else
                {
                    Log.Debug($"Account with Id={accountprofile.AccountId} try to sign in at {DateTime.UtcNow}");
                    var result = await _accountService.ExternalSigninAsync(externalUser, accountprofile).ConfigureAwait(false);

                    switch (result.Code)
                    {
                    case 200:
                        return(Ok(result.Data));

                    case 500:
                        return(Problem(_localizer[result.Message]));

                    default:
                        return(BadRequest(_localizer[result.Message]));
                    }
                }
            }
            catch (Exception ex) {
                Log.Error(ex, ex.Source);
                return(Problem(_localizer[DataTransferer.SomethingWentWrong().Message]));
            }
        }