[英]Deployed Blazor server to localhost, Default Login page (cshtml) Error
I have deployed a Blazor server site to localhost/store and make required adjustments, The web site works but there is a problem with its Login.cshtml page, Its GetAsync method works good, and it has the expected web address: http://localhost/store/identity/account/login/?lang=en&returnUrl=%2fstore%2fcoronavirus But when I enter my user name and password it says: "Sorry, there's nothing at this address.", and shows the same address when it first loaded: http://localhost/store/identity/account/login/?lang=en&returnUrl=%2fstore%2fcoronavirus The code for Login page is: I have deployed a Blazor server site to localhost/store and make required adjustments, The web site works but there is a problem with its Login.cshtml page, Its GetAsync method works good, and it has the expected web address: http:// localhost/store/identity/account/login/?lang=en&returnUrl=%2fstore%2fcoronavirus 但是当我输入我的用户名和密码时,它会说:“对不起,这个地址没有任何东西。”,并在第一次显示相同的地址已加载:http://localhost/store/identity/account/login/?lang=en&returnUrl=%2fstore%2fcoronavirus 登录页面的代码为:
[AllowAnonymous]
public class LoginModel : PageModel
{
private readonly UserManager<AppUser> _userManager;
...
public LoginModel(UserManager<AppUser> userManager, ...)
{
_userManager = userManager;
...
}
[BindProperty]
public InputModel Input { get; set; }
public IList<AuthenticationScheme> ExternalLogins { get; set; }
[BindProperty]
public string ReturnUrl { get; set; } = null;
[BindProperty]
public string Lang { get; set; } = "fa";
[TempData]
public string ErrorMessage { get; set; }
public class InputModel
{
[Required]
[MaxLength(256)]
public string User { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
public async Task OnGetAsync([FromQuery]string returnUrl = null, [FromQuery] string lang = "en")
{
if (!string.IsNullOrEmpty(ErrorMessage))
{
ModelState.AddModelError(string.Empty, ErrorMessage);
}
returnUrl = returnUrl ?? Url.Content("");
// Clear the existing external cookie to ensure a clean login process
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
ReturnUrl = returnUrl;
Lang = lang;
}
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
AppUser user = await _userManager.FindByNameAsync(Input.User);
if (user == null)
{
ModelState.AddModelError(string.Empty, "User not found.");
GetLanguageValues(Lang);
return Page(); // <= I think problem is here
}
if (!await _userManager.IsEmailConfirmedAsync(user))
{
ModelState.AddModelError(string.Empty, "You didn't confirmed your Email.");
GetLanguageValues(Lang);
return Page();
}
if (!user.IsApproved)
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
GetLanguageValues(Lang);
return Page();
}
var result = await _signInManager.PasswordSignInAsync(Input.User, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
user.LastLoginDate = DateTime.Now;
await _userManager.UpdateAsync(user);
return LocalRedirect(ReturnUrl);
}
if (result.IsLockedOut)
{
ModelState.AddModelError(string.Empty, "You are Locked Out.");
GetLanguageValues(Lang);
return Page();
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
GetLanguageValues(Lang);
return Page();
}
}
// If we got this far, something failed, redisplay form
GetLanguageValues(Lang);
return Page();
}
since the browser shows the same address after pressing login, I think a "return Page()" is executed and then OnPostAsync() should execute but it is not found, if it is a succcess the browser should redirect to "returnUrl" and show that url.由于浏览器在按下登录后显示相同的地址,我认为执行了“return Page()”,然后应该执行 OnPostAsync() 但没有找到,如果成功,浏览器应该重定向到“returnUrl”并显示url。 I also render Model properties in the view?
我还在视图中呈现 Model 属性? why I get this error.
为什么我会收到此错误。 all parts work except cshtml (it works when I run the project in Visual Studio).
除了 cshtml 之外的所有部分都可以工作(当我在 Visual Studio 中运行项目时它可以工作)。
I changed all src and href in shared\_Layout.cshtml, so that they are not begin with '/' slash and now it works.我更改了 shared\_Layout.cshtml 中的所有 src 和 href,因此它们不是以 '/' 斜杠开头的,现在它可以工作了。
Edit:编辑:
After Try and error I realized that src and hrefs in the cshtml files should begin with "~/" and in the blazor main app should be like this: "css/aaa/bbb/c.css" so don't begin with a slash.在尝试和错误之后,我意识到 cshtml 文件中的 src 和 hrefs 应该以“~/”开头,而在 blazor 主应用程序中应该是这样的:“css/aaa/bbb/c.css”所以不要以削减。 For the original problem, It seems return Redirect(ReturnUrl) is correct and in edge, when I press login button, the correct url appears for a second in address bar and then comes back to original page (Login page).
对于原始问题,似乎返回重定向(ReturnUrl)是正确的并且在边缘,当我按下登录按钮时,正确的 url 在地址栏中出现一秒钟,然后返回原始页面(登录页面)。
It seems the browser can not read the set auth cookie, and the authentication if statement in the target page, redirects back to Login page.浏览器似乎无法读取设置的身份验证cookie,并且目标页面中的身份验证if语句重定向回登录页面。
I tried this in Edge Canary and Chrome and in Firefox and in these browsers redirection from login page to target page worked correctly with Auth cookie set.我在 Edge Canary 和 Chrome 以及 Firefox 中尝试了此操作,并且在这些浏览器中,从登录页面重定向到目标页面的重定向与 Auth cookie 集一起正常工作。 But in edge there is a problem.
但是在边缘有一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.