无法运行Azure Web作业错误:Microsoft.IdentityModel.SecurityTokenService.RequestFailedException:令牌请求失败

时间:2017-04-28 16:47:15

标签: azure azure-webjobs sharepoint-online

我们有一个每周运行的Azure Web作业。但最近它还没有运行。当我们检查日志时,这就是我们得到的

    [04/28/2017 16:32:38 > e85929: SYS INFO] Status changed to Initializing

[04/28/2017 16:32:40 > e85929: SYS INFO] Run script 'EmailSchedulerJob.exe' with script host - 'WindowsScriptHost'

[04/28/2017 16:32:40 > e85929: SYS INFO] Status changed to Running

[04/28/2017 16:32:40 > e85929: INFO] Site URL is https://name.sharepoint.com

[04/28/2017 16:32:41 > e85929: INFO] Error: Microsoft.IdentityModel.SecurityTokenService.RequestFailedException: Token request failed. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.

[04/28/2017 16:32:41 > e85929: INFO]    at System.Net.HttpWebRequest.GetResponse()

[04/28/2017 16:32:41 > e85929: INFO]    at Microsoft.IdentityModel.S2S.Protocols.OAuth2.OAuth2WebRequest.GetResponse()

[04/28/2017 16:32:41 > e85929: INFO]    at Microsoft.IdentityModel.S2S.Protocols.OAuth2.OAuth2S2SClient.Issue(String securityTokenServiceUrl, OAuth2AccessTokenRequest oauth2Request)

[04/28/2017 16:32:41 > e85929: INFO]    --- End of inner exception stack trace ---

[04/28/2017 16:32:41 > e85929: INFO]    at Microsoft.IdentityModel.S2S.Protocols.OAuth2.OAuth2S2SClient.Issue(String securityTokenServiceUrl, OAuth2AccessTokenRequest oauth2Request)

[04/28/2017 16:32:41 > e85929: INFO]    at EmailSchedulerJob.TokenHelper.GetAppOnlyAccessToken(String targetPrincipalName, String targetHost, String targetRealm)

[04/28/2017 16:32:41 > e85929: INFO]    at EmailSchedulerJob.Program.Main(String[] args)

[04/28/2017 16:32:41 > e85929: SYS INFO] Status changed to Success

我认为这与代码有关。我们发布了这个项目的多个版本,但仍然遇到了同样的问题。该代码在SharePoint列表上执行查找。

我们还尝试查看“发布设置”文件。它与控制面板的链接似乎略有不同

<publishData><publishProfile profileName="TestAppName - Web Deploy" publishMethod="MSDeploy" publishUrl="testappname.scm.azurewebsites.net:443" msdeploySite="estAppName" userName="$TestAppName" userPWD="" destinationAppUrl="http://appname.azurewebsites.net" SQLServerDBConnectionString="" mySQLDBConnectionString="" hostingProviderForumLink="" controlPanelLink="http://windows.azure.com" webSystem="WebSites"><databases /></publishProfile><publishProfile profileName="TestAppName - FTP" publishMethod="FTP" publishUrl="ftp://waws-prod-sn1-031.ftp.azurewebsites.windows.net/site/wwwroot" ftpPassiveMode="True" userName="TestAppName\$TestAppName" userPWD="" destinationAppUrl="http://testappname.azurewebsites.net" SQLServerDBConnectionString="" mySQLDBConnectionString="" hostingProviderForumLink="" controlPanelLink="http://windows.azure.com" webSystem="WebSites"><databases /></publishProfile></publishData>

非常感谢有关如何解决这一问题的任何建议。

修改 初始化Web作业的代码 webjob-发布 - settings.json

{
  "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
  "webJobName": "EmailSchedulerJob",
  "startTime": "2016-05-30T00:00:00-05:00",
  "endTime": null,
  "jobRecurrenceFrequency": "Week",
  "interval": 1,
  "runMode": "Scheduled"
}

web.config文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <appSettings>
    <add key="ClientId" value=""/>
    <add key="ClientSecret" value=""/>
    <add key="DaysLeftToRemind" value="3"/>
  </appSettings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <!--Used by SharePoint Add-in-->
        <binding name="secureBinding">
          <security mode="Transport"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="https" bindingConfiguration="secureBinding"/>
    </protocolMapping>
  </system.serviceModel>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>

<system.diagnostics>
    <sources>
      <source name="System.Net" tracemode="includehex" maxdatasize="1024">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="System.Net" value="Verbose"/>
      <add name="System.Net.Sockets" value="Verbose"/>
      <add name="System.Net.Cache" value="Verbose"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="D:\home\LogFiles\NetTracing\network.log"
           traceOutputOptions="ProcessId, DateTime"
      />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
</configuration>

连接到SharePoint的代码

string sharepointSiteUrl = ConfigurationManager.AppSettings.Get("SPSiteUrl");
                string emailUserName = ConfigurationManager.AppSettings.Get("EmailUserName"); 
                string emailPassword = ConfigurationManager.AppSettings.Get("EmailPassword");
                Uri sharepointSiteUri = new Uri(sharepointSiteUrl);

                Console.WriteLine("Site URL is " + sharepointSiteUrl);

                string appRealm = TokenHelper.GetRealmFromTargetUrl(sharepointSiteUri);
                string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, sharepointSiteUri.Authority, appRealm).AccessToken;

更新

由于客户端密钥可能已过期,我们生成了一个新密钥,现在当我们运行Web作业时,我们在日志中收到此错误 -  错误:Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:访问被拒绝。您无权执行此操作或访问此资源。

当前日志文件

[05/01/2017 20:07:19 > 56e469: SYS INFO] Status changed to Initializing

    [05/01/2017 20:07:19 > 56e469: SYS INFO] Run script 'EmailSchedulerJob.exe' with script host - 'WindowsScriptHost'

    [05/01/2017 20:07:19 > 56e469: SYS INFO] Status changed to Running

    [05/01/2017 20:07:20 > 56e469: INFO] Site URL is https://sohodragon.sharepoint.com

    [05/01/2017 20:07:20 > 56e469: INFO] Inside hostClientContext

    [05/01/2017 20:07:21 > 56e469: INFO] Error: Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource.

    [05/01/2017 20:07:21 > 56e469: INFO]    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)

    [05/01/2017 20:07:21 > 56e469: INFO]    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()

    [05/01/2017 20:07:21 > 56e469: INFO]    at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)

    [05/01/2017 20:07:21 > 56e469: INFO]    at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()

    [05/01/2017 20:07:21 > 56e469: INFO]    at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()

    [05/01/2017 20:07:21 > 56e469: INFO]    at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

    [05/01/2017 20:07:21 > 56e469: INFO]    at EmailSchedulerCommon.Code.Helpers.SharePointHelpers.GetListItemsByViewName(ClientContext ctx, String listName, String viewName)

    [05/01/2017 20:07:21 > 56e469: INFO]    at EmailSchedulerCommon.Code.Business.HolidayManager.GetUpcomingHolidays(ClientContext ctx)

    [05/01/2017 20:07:21 > 56e469: INFO]    at EmailSchedulerJob.Program.Main(String[] args)

[05/01/2017 20:07:21 > 56e469: SYS INFO] Status changed to Success

1 个答案:

答案 0 :(得分:0)

该问题已得到修复。删除了旧的app文件。使用新密钥部署并重新添加新的应用程序文件。