下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

编程语言如何安全连接Office 365 Online

作者:李建辉     来源: 世纪互联蓝云点击数:1252发布时间: 2020-01-13 10:22:37

标签: 编程语言Java视频课程Javascript视频

大神带你学编程,欢迎选课

如何安全连接Office 365 Online。在过去的几十年间,大量的编程语言被发明、被取代、被修改或组合在一起。尽管人们多次试图创造一种通用的程序设计语言,却没有一次尝试是成功的。之所以有那么多种不同的编程语言存在的原因是,编写程序的初衷其实也各不相同;新手与老手之间技术的差距非常大,而且有许多语言对新手来说太难学;还有,不同程序之间的运行成本(runtime cost)各不相同。

随着Office 365 在中国的迅速普及,越来越多的公司开始使用Office 365及相关服务。能够熟练使用并管理Office 365 就成为广大公司IT管理员的一个必备技能。

编程语言如何安全连接Office 365 Online_编程语言_Java视频课程_Javascript视频_课课家

今天我们就来介绍一种较为安全便捷的方式的连接Office 365 Online,即在PowerShell界面,通过加密用户名和密码的方式连接Office 365 Online。那我们使用PowerShell对Office 365 Online进行远程管理,有如下优点:

  • Office 365 拥有仅可使用 Office 365 PowerShell 配置的功能
  • Office 365 PowerShell 善于执行批量操作
  • Office 365 PowerShell 善于筛选数据
  • Office 365 PowerShell 方便打印或保存数据
  • Office 365 PowerShell 支持跨服务器产品管理
  • Office 365 PowerShell 会显示无法通过 Microsoft 365 管理中心看到的其他信息

在连接过程中,如果用户名和密码以明文形式输入,就会带来安全风险。如果采用以下PowerShell脚本就可以避免这个缺点:预先定义两个函数,分别用于加密和解密字符串;然后检查本地是否存在已经加密的用户名和密码文件,如果没有,提示用户输入用户名和密码,并将其以密文形式存到本地;最后,读取本地加密的用户名和密码,并将其解密,用于远程连接Office 365 Online。

脚本代码分为以下三个部分介绍给大家。

第一部分,定义加密和解密的函数。

  1. # This function is to encrypt a string. 
  2. function Encrypt-String($String, $Passphrase, $salt="SaltCrypto", $init="IV_Password", [switch]$arrayOutput)  
  3. {  
  4.     $r = new-Object System.Security.Cryptography.RijndaelManaged  
  5.     $pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)  
  6.     $salt = [Text.Encoding]::UTF8.GetBytes($salt)  
  7. $r.Key = (new-Object ` 
  8.                   Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32)  
  9. $r.IV = (new-Object ` 
  10.               Security.Cryptography.SHA1Managed).ComputeHash ` 
  11.               [Text.Encoding]::UTF8.GetBytes($init) )[0..15]  
  12.     $c = $r.CreateEncryptor()  
  13.     $ms = new-Object IO.MemoryStream  
  14.     $cs = new-Object Security.Cryptography.CryptoStream $ms,$c,"Write"  
  15.     $sw = new-Object IO.StreamWriter $cs  
  16.     $sw.Write($String)  
  17.     $sw.Close()  
  18.     $cs.Close()  
  19.     $ms.Close()  
  20.     $r.Clear()  
  21.     [byte[]]$result = $ms.ToArray()  
  22.     return [Convert]::ToBase64String($result)  
  23. }  
  24.  
  25. # This function is to de-encrypt a string. 
  26. function Decrypt-String($Encrypted, $Passphrase, $salt="SaltCrypto", $init="IV_Password")  
  27. {  
  28.     if($Encrypted -is [string]){  
  29.         $Encrypted = [Convert]::FromBase64String($Encrypted)  
  30.        }  
  31.     $r = new-Object System.Security.Cryptography.RijndaelManaged  
  32.     $pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)  
  33.     $salt = [Text.Encoding]::UTF8.GetBytes($salt)  
  34. $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes ` 
  35.                  $pass, $salt, "SHA1", 5).GetBytes(32) 
  36. $r.IV = (new-Object ` 
  37.               Security.Cryptography.SHA1Managed).ComputeHash ` 
  38.               ( [Text.Encoding]::UTF8.GetBytes($init) )[0..15]  
  39.     $d = $r.CreateDecryptor()  
  40.     $ms = new-Object IO.MemoryStream @(,$Encrypted)  
  41.     $cs = new-Object Security.Cryptography.CryptoStream $ms,$d,"Read"  
  42.     $sr = new-Object IO.StreamReader $cs  
  43.     Write-Output $sr.ReadToEnd()  
  44.     $sr.Close()  
  45.     $cs.Close()  
  46.     $ms.Close()  
  47.     $r.Clear()  
  48. Clear-Host 

第二部分,从本地的文本文件中读取加密的Office 365用户名和密码。只第一次需要手工输入用户名和密码,然后将加密的用户名和密码以密文形式存储到本地磁盘。此后无需输入。

  1. #Try to read the encrypted user name and password from the specific path, if there are, read and de-encrypt them. If there are not, prompt for input and encrypt them. 
  2. $uencrypted = Get-Content -ErrorAction SilentlyContinue -Path 'C:\\$Home\\Desktop\\Username.txt' 
  3. $pencrypted = Get-Content -ErrorAction SilentlyContinue -Path 'C:\\$Home\\Desktop\\password.txt' 
  4. If ($null -ne $uencrypted -and $null -ne $pencrypted) 
  5.     $udecrypted = Decrypt-String $uencrypted "U_MyStrongPassword" 
  6.     $pdecrypted = Decrypt-String $pencrypted "P_MyStrongPassword" 
  7.     $pdecrypted = ConvertTo-SecureString $pdecrypted -AsPlainText -Force 
  8. Else 
  9.     $ustring = read-host "Please Enter Office 365 User name"  
  10.     $pstring = read-host "Please Enter Office 365 User Password"  
  11.     $uencrypted = Encrypt-String $ustring "U_MyStrongPassword" 
  12.     $uencrypted | Out-File "$HOME\\Desktop\\Username.txt" 
  13.     write-host "Store the encrypted Username successfully!"  
  14.     $pencrypted = Encrypt-String $pstring "P_MyStrongPassword" 
  15.     $pencrypted | Out-File "$HOME\\Desktop\\password.txt" 
  16.     write-host "Store the encrypted password successfully!" 
  17.     $udecrypted = Decrypt-String $uencrypted "U_MyStrongPassword" 
  18.     $pdecrypted = Decrypt-String $pencrypted "P_MyStrongPassword" 
  19.     $pdecrypted = ConvertTo-SecureString $pdecrypted -AsPlainText -Force 

第三部分,连接Office 365 Online。 执行以下命令后,就可以在PowerShell下,远程管理Office 365 Exchange Online了。

  1. #Connect to Office 365 online or Azure 
  2. $LiveCred = New-Object System.Management.Automation.PSCredential $udecrypted, $pdecrypted     
  3. $Session = New-PSSession -ConfigurationName Microsoft.Exchange ` 
  4.                      -ConnectionUri https://partner.outlook.cn/powershell -Credential $LiveCred ` 
  5.                      -Authentication BASIC –AllowRedirection -ErrorAction Stop ` 
  6.                      -Name "$($Credential.UserName)" 
  7. Import-PSSession $Session 
  8. Connect-MsolService –Credential $LiveCred -AzureEnvironment AzureChinaCloud 

注意:执行最后一个命令,需要预先安装Microsoft Online Services Sign-In Assistant。安装方法可自行百度,本篇不做介绍。

编程语言往往使程序员能够比使用机器语言更准确地表达他们所想表达的目的。对那些从事计算机科学的人来说,懂得程序设计语言是十分重要的,因为在当今所有的计算都需要程序设计语言才能完成。

赞(14)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程