使用Powershell进行基本身份验证实施的动态用户凭据生成

时间:2018-03-26 06:30:28

标签: powershell azure authentication azure-web-sites azure-powershell

要求:希望使用自定义php脚本进行基本身份验证设置。 想:

  
      
  • 动态创建用户凭据
  •   
  • 创建更新了这些凭据的cred php文件
  •   
  • 更新用户名& Azure WebApp设置的相应密码。
  •   

[ 注意 :如果遗漏将在即将发布的帖子中自动传送cred和auth文件

1 个答案:

答案 0 :(得分:0)

工具&先决条件:
Azure Powershell 4.0+
Windows Powershell ISE
存储帐户知识(名称,RG' s)
了解需要备份的WebApp(WebApp名称和RG)
有效& Active Azure门户/ AD登录凭据

结果 在Azure门户网站上,关注的网络应用程序将具有“global_cred”#39;使用凭据生成的变量密钥集在应用程序设置部分中以[用户名:密码]格式存储为其值。

            #######################
            # Function to Generate dynamic 22char random string for password use
            # Call custom function to write php constants for basic auth 
            Function writeFocusDomainCredFile{

                param( $configObject, $hash, $farmprojectname )
                #setting auth username:password
                $configObject.authPasswordKey = (Get-RandomAlphanumericString -length 22 | Tee-Object -variable teeTime )
                $hash['global_cred'] = [String]( $configObject.authUsernameKey + ':' + $configObject.authPasswordKey )
                $prfilename = ( $configObject.ftpappdirectory + '\cred.php')
                writeProjectBasicAuthCredOnNew -filename $prfilename -configObject $configObject
            }

            ##########################
            # Function to write the credentials to cred php file for basic auth use
            # This file with other dependent files could be automatically ftp'd. 
            # Would share in another post 
            Function writeProjectBasicAuthCredOnNew{
               param( $filename
                      ,$configObject
                      )

                writeDeployFileFiltersForDomain -ReportFileName $filename
                Add-Content $filename ( "<?php" )
                Add-Content $filename ("define('WPIZED_AUTH_USER', '" + $configObject.authUsernameKey + "');"); 
                Add-Content $filename ("define('WPIZED_AUTH_PASS', '" + $configObject.authPasswordKey + "');");   
            }

            ###################################################################################################
            # Configure Below as You prefer or desire #


            $properties = @{
                            'ResourceName' = "AzureAppName";
                            'myResourceGroupName' = "{App Resource Group Name}"; 
                            'mySubscriptionName' = "{subscription name}"; 
                            'adminEmail' = "H.Bala@volunteering.com";
                            'ResourceGroupLocation' = "East US";      
                            'authUsernameKey' = 'HBalaUsername'; #For this post, using fixed username as 'HBalaUsername'
                            'authPasswordKey' = '';
                            'PathFormatDate' = Get-Date -UFormat "%Y_%m_%d";

                       }
            $configObject = New-Object –TypeName PSObject –Prop $properties
            Write-Output $configObject



            #Login cmdlet for active session
            Login-AzureRmAccount
            Get-AzureRmSubscription –SubscriptionName $configObject.mySubscriptionName | Select-AzureRmSubscription 
            (Get-AzureRmContext).Subscription
            Select-AzureRMSubscription -SubscriptionName $configObject.mySubscriptionName 

            #Pull the Webapp details and configuration
            $webApp = Get-AzureRMWebApp -ResourceGroupName $configObject.myResourceGroupName -Name $configObject.ResourceName 
            #Pull the Application Listing Environment / Configuration Variables
            $appSettingList = $webApp.SiteConfig.AppSettings
            $hash = @{}
            ForEach ($kvp in $appSettingList) {
               $hash[$kvp.Name] = $kvp.Value
            }


            writeFocusDomainCredFile -configObject $configObject -hash $hash
            #[FTP Deploy Logic of this file and other basic auth or files shall cover in seperate topic ]
            #[ Only setting the generated Credentials and saving to Application setting focused here ]
            Set-AzureRMWebApp -ResourceGroupName $configObject.myResourceGroupName -Name $configObject.ResourceName -AppSettings $hash  

免责声明目的是与另一位可能觉得有帮助的新手分享。