Asp.Net Core 3.0 应用程序。我正在使用实体框架身份个人身份验证,并在登录页面和注册页面中搭建了脚手架。我在 Azure 中有一个 SQL 数据库。
当我在 localhost 上运行时,注册和登录工作正常。我已经确认新用户存储在 DB Users 表中。
但是当我部署到 Azure AppService时,单击注册或登录按钮时出现 404 page not found 错误。
我努力了:
扩大到付费计划并使用 GoDaddy 和 Azure 为我的应用服务实施 CNAME,以
.azurewebsites.net
从 url中删除添加
asp-area
到我的cshtmlasp-page
中的元素。<button>
在应用服务中打开 Azure 身份验证和授权并添加
http://www.myapp.com
为重定向 url。在我的 Azure DB 服务器防火墙设置中将我的所有 AppService 出站 IP 列入白名单。
我曾尝试在 Chrome 中使用开发控制台来查看发生了什么,但我看不到任何有用的东西。
拔掉了我的一些头发...
我应该在哪里看的任何想法?
附加信息:
应用设置.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=tcp:**-*****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
数据库上下文:
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace MyApp.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
}
}
注册cshtml:
<button type="submit" class="btn btn-primary" asp-area="Identity" asp-page="Register" id="RegisterButton">@_loc[Model.RegisterButtonName]</button>
登录cshtml:
<div class="form-group">
<button type="submit" class="btn btn-primary" asp-area="Identity" asp-page="Login" id="LoginButton">@_loc[Model.LoginButtonName]</button>
</div>
错误:如果我从主页导航到https://www.myapp.com/en/Identity/Account/Login ,页面加载正常。但是当我单击登录/注册按钮时,重定向失败并显示以下错误。
中间件:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseRequestLocalization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default", pattern: "
{culture=en}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Features", pattern: "
{culture=en}/{controller=Features}/{action=Features}/{id?}");
endpoints.MapControllerRoute(
name: "About", pattern: "
{culture=en}/{controller=About}/{action=About}/{id?}");
endpoints.MapControllerRoute(
name: "Help", pattern: "
{culture=en}/{controller=Help}/{action=Help}/{id?}");
endpoints.MapRazorPages();
});
数据库:
单击注册时数据库中没有创建用户记录,只是错误页面
更新:
我在整个代码库中搜索了“localhost”,但在 IIS 设置之外的任何地方都没有找到它。
更新:
我将 nslookup 中的服务器和虚拟入站 IP 地址添加到数据库服务器防火墙白名单中。
更新:
在 Firefox 上,我没有收到错误消息。只是一个空白的白页。
在 Edge 上,我收到与找不到页面相同的 404 错误。
更新:
在我的 startup.cs 服务中,我true
尝试false
了RequireConfirmedAccount
services.AddDefaultIdentity<ApplicationUser>( options => { options.SignIn.RequireConfirmedAccount = true; }).AddEntityFrameworkStores<ApplicationDbContext>();
更新:
确认我的 cshtml 属性的 Build Action 设置为 Content。
更新:
添加TXT | 阿苏德| 应用验证 ID到我在 GoDaddy 中的 DNS 设置。
添加TXT | @ | mywebsite.azurewebsites.net到我在 GoDaddy 中的 DNS 设置。
添加 mywebsite.com 作为分配的自定义域并在 azure 门户中对其进行验证。
更新:
我在 MSDN 论坛上被告知这可能是因为提交按钮的 http POST 不起作用,我应该将其更改为锚<a>
标记或Input
类型按钮。
更改 html 很好,但我想弄清楚这对我的 Razor 页面重定向代码意味着什么?如果有人能指出我正确的方向,那就太好了。注册页面的现有重定向代码如下所示。
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl ??= Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code },
protocol: Request.Scheme);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
return RedirectToPage("RegisterConfirmation", new { email = Input.Email });
}
else
{
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
我从一些研究中猜测我的新 cshtml 将如下所示:
<input id="RegisterButton" runat="server" onserverclick="RegisterButtonClick" type="submit" class="btn btn-primary" asp-area="Identity" asp-page="Register" >@_loc[Model.RegisterButtonName]</input>
更新:
我用我的方法注释掉了Use.Hsts
prodConfigure
startup.cs
更新:
我尝试returnUrl
在OnPostAsync
我的方法中对a 进行硬编码,login.cshtml.cs
而不是使用returnUrl ??= Url.Content("~/");
相同的 404 错误……所以我不确定重定向到同一登录页面的身份服务实际上在哪里触发了重定向。显然不在我的 OnPostAsync 中,因为它应该尝试访问我硬编码的 url,即~/myapp.com/en/Features/Features
. 啊。这让我很头疼。
更新:
我尝试替换return LocalRedirect(returnUrl);
为,
return LocalRedirect(Url.Action("Login", "Identity/Account"));
但它在 localhost 上不起作用,所以我没有在 prod 中尝试过。它在说
No webpage was found for the web address: https://localhost:44305/en/Identity%2FAccount/Login
更新:
returnUrl ??= Url.Content("~/");
我用运算符 for替换了空合并运算=
符 forreturnUrl = Url.Content("~/");
更新:
确认默认情况下我的AddControllersWithViews
和AddRazorPages
不全局实施授权,并且我的登录和注册页面模型已 [AllowAnonymous]
指定。
更新:
我已经对我的OnPostAsync
方法和我的ConfigureServices<IdentityOptions>
. 我认为默认的 IdentityOptions 密码要求掩盖了真正的问题。
我仍然可以使用我在搭建脚手架之前创建的用户在 localhost 上正常登录,但我无法使用该用户登录App Service。但现在在 AppService 上,我在该预脚手架用户的 UI 上收到“无效登录尝试”错误。控制台中没有错误。
所以基本上我仍然无法在 AppService 上注册或登录,但不知何故 2 周后,我可以尝试诊断的 UI 上出现错误消息,感觉就像是进步了!
我将对此进行调查,因为它也发生在我在 localhost 中注册的新用户身上,所以无论如何都需要先解决它。如果它没有帮助,我将研究 CORS 作为下一站以找到 AppService 解决方案。我也向微软伸出了手,手指交叉。谢谢各位。
更新:现在已解决。老实说,我不知道怎么做。在过去的几周里,我做了很多改变,一一撤消我的所有提交,以找出修复的内容太痛苦了。当痛苦的记忆在几周内消退时,我会回来尝试再次打破它以找出它是什么。到时我会发布更新。