【问题标题】:Publishing Azure App Service via Visual Studio fails通过 Visual Studio 发布 Azure 应用服务失败
【发布时间】:2018-02-09 10:24:54
【问题描述】:

我将在Publish the server project 之后发布我的 Azure 应用服务(.Net 后端)

以前可以,但现在我不能让它工作了。当我在 Visual Studio 中单击发布时,它会构建并显示

"========== Publish: 1 succeeded, 0 failed, 0 skipped =========="

但是在之后打开的网络浏览器选项卡中(通常会出现“此移动应用程序已启动并正在运行”屏幕)它会将我发送到https://login.microsoftonline.com/{domain}.onmicrosoft.com/oauth2/v2.0/authorize?response_type=...,然后立即将我重定向到msala{custom-redirect-uri}://auth,这是重定向我的本机应用中 Azure AD B2C 的 URI。

在 Azure 门户中我的应用服务的活动日志中,我也没有看到更新。当我查询数据库时,结构并没有按应有的方式改变。

那么那里发生了什么以及为什么重定向到自定义重定向 URI? Azure AD B2C 与发布后端项目有什么关系?

更新:

检查打开的网页,我看到以下内容:

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' >  
<html xmlns = 'http://www.w3.org/1999/xhtml' >  
<head>  
<title> Logging in... </title> 
<meta name = 'CACHE-CONTROL' content = 'NO-CACHE' />  
<meta name = 'PRAGMA' content = 'NO-CACHE' /> 
<meta name = 'EXPIRES' content = '-1' />
</head> 
<body > 
<form id = 'auto' method = 'post' action = 'msal{Client_id}://auth'>  
<div> 
<input type = 'hidden' name = 'error' id = 'error' value = 'redirect_uri_mismatch' /> 
<input type = 'hidden' name = 'error_description' id = 'error_description' value = 'AADB2C90006: The redirect URI &#39;https://{domain}.azurewebsites.net/.auth/login/aad/callback&#39; provided in the request is not registered for the client id &#39;{Client_id}&#39;.
Correlation ID: 9569081c-2779-41e8-8b20-095384de402f
Timestamp: 2018-02-06 18:53:38Z
'/> 
<input type = 'hidden' name = 'state' id = 'state' value = 'redir=%2F' />  
</div>  
<div id = 'noJavascript' style = 'visibility: visible; font-family: Verdana' > 
<p> Although we have detected that you have Javascript disabled, you will be able to use the site as normal. </p> 
<p> As part of the authentication process this page may be displayed several times.Please use the continue button below. </p> 
<input type = 'submit' value = 'Continue' /> 
</div> 
<script type = 'text/javascript' >
   < !--
   document.getElementById('noJavascript').innerHTML = '';
   document.getElementById('auto').submit();
   //-->
</script></form></body></html>

我仍然看不到从我的本机应用程序客户端(客户端 ID)到我的 .Net 后端应用程序服务的连接。当然它们是连接的,但帽子不应该对发布他的后端有任何影响

【问题讨论】:

    标签: azure azure-mobile-services azure-ad-b2c azure-web-app-service


    【解决方案1】:

    'error_description' id = 'error_description' value = 'AADB2C90006: 重定向 URI 'https://{domain}.azurewebsites.net/.auth/login/aad/callback'请求未注册客户端 ID '{Client_id}'

    如果我对您的理解正确,您说的是 Xamarin 应用程序。通常,我们会在 Azure Active Directory B2C 租户中注册一个移动/本机应用程序,并设置自定义重定向 URI/重定向 URI。详情请关注Register your application

    然后我们将在移动客户端中使用 MSAL 来检索令牌,并使用Client-managed authentication 发送令牌以向移动应用后端进行身份验证,如下所示:

    var payload = new JObject();
    if (authenticationResult != null && !string.IsNullOrWhiteSpace(authenticationResult.IdToken))
    {
       payload["access_token"] = authenticationResult.IdToken;
    }
    var user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory,payload);
    

    根据您的描述,我假设您已在移动应用的“设置 > 身份验证/授权”部分下将 请求未通过身份验证时采取的操作设置为 Log in with Azure Active Directory。默认情况下,如果您通过浏览器访问您的手机,它将使用Server-managed authentication,此时您需要在您的 AAD 应用的 Redirect URIs 下设置https://{your-mobileapp-name}.azurewebsites.net/.auth/login/aad/callback

    另外,这里有一些有用的教程,你可以参考一下:

    TodoAzureAuthADB2CClientFlow

    Authenticating Users with Azure Active Directory B2C

    TodoAzureAuthADB2CServerFlow

    注意:对于服务器流示例,您不需要使用 MSAL,只需使用特定提供商直接再次登录您的移动应用程序,如下所示:

    var user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, Constants.URLScheme);
    

    并且您需要在 Azure 门户上为您的移动客户端应用程序 Add your app to the Allowed External Redirect URLs

    更新:

    但是,我所做的更改并没有反映在他的 Azure DB 中。但这似乎是应用程序启动时 EF Migration 的问题,必须先配置它

    对于 EF 迁移,如果您禁用自动迁移,默认情况下您的数据库初始化程序将是CreateDatabaseIfNotExists,当您的应用第一次访问数据库时,如果不存在,它将创建数据库。或者您可以启用自动迁移并在您的 Startup.MobileApp.cs 文件的ConfigureMobileApp 方法下调用new DbMigrator(new Migrations.Configuration()).Update(),此时更改将在应用程序启动时应用。

    由于您使用的是 ClientFlow 并且不要在 AAD 应用程序的重定向 URI 下配置 https://{your-mobileapp-name}.azurewebsites.net/.auth/login/aad/callback。在通过 VS 将应用程序发布到 Azure 端之前,您可以在您的 &lt;your-mobile-app-name&gt; - Web Deploy.pubxml 文件下将 LaunchSiteAfterPublish 设置为 False,因为您已评论禁用在发布成功完成后自动启动您的网站。

    【讨论】:

    • 我使用 ClientFlow 并且效果很好。将后端(应用服务/移动应用)发布到 Azure 时出现错误。但我认为我有问题。应用程序发布后,应用程序将在浏览器窗口中打开。而且由于它未配置为在浏览器中工作,因此他的重定向也不起作用。我现在只是禁止在发布后在浏览器中打开应用程序。我曾经认为打开浏览器窗口对发布操作有任何影响,但实际上没有。
    • 但是,我所做的更改并没有反映在他的 Azure DB 中。但这似乎是应用程序启动时 EF 迁移的问题,必须首先配置。在 Xamarin 后端的任何介绍性示例中,我都没有阅读任何相关内容,甚至示例应用程序也未配置为自动迁移。所以我必须先解决这个问题。
    • 对于 EF 迁移,如果你禁用自动迁移,默认情况下你的数据库初始化器将是CreateDatabaseIfNotExists,当你的应用第一次访问数据库时,如果不存在,它将创建数据库。或者您可以启用自动迁移并在 Startup.MobileApp.cs 文件的 ConfigureMobileApp 方法下调用 new DbMigrator(new Migrations.Configuration()).Update()
    • 或者您可以在将移动应用后端发布到 Azure 移动端之前手动执行数据库迁移。详情可以关注阿德里安·霍尔的书adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/…
    • 我会看看你的两种方法,谢谢。我已经尝试过自动迁移,但没有成功。
    猜你喜欢
    • 2022-12-10
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多