When I try to make an HTTPS request with my Razor Page eg. a POST request then comes in the console:
info: Microsoft.AspNetCore.Hosting.Internal.WebHost [2]
Request finished in 103.9478ms 200 text / html; charset = utf-8
but the website just keeps loading and I will not be redirected to the index page
here is the code from the post method
public async Task<IActionResult> OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
if (this.RegisterViewModel.Password == this.RegisterViewModel.ConfirmPassword)
{
var profileResult = await this.dbContext.Profiles.AddAsync(new Profile
{
Id = Guid.NewGuid(),
Created = DateTime.Now,
Modified = DateTime.Now
});
var userResult = await this.userManager.CreateAsync(new ApplicationUser
{
UserName = this.RegisterViewModel.UserName,
Email = this.RegisterViewModel.Email,
ProfileId = profileResult.Entity.Id
}, this.RegisterViewModel.Password);
if (!userResult.Succeeded)
{
ModelState.AddModelError("", "Invalid credentials.");
return Page();
}
} else
{
ModelState.AddModelError("", "Passwords dosenĀ“t match.");
return Page();
}
return RedirectToPage("/Index");
}
Related
Currently, I am working on a web application using MVC C#. I would like to ask if there is a way to implement a PHP Session in MVC. The use of the session I am talking about is like, when you write only the link or path like this from https://localhost:44360/Login/Login to https://localhost:44360/Home/Home to go to the Home Page of the Web even if you did not sign-in using an account you will not be redirected to that page.
After I logged in to an account, I am redirected to the homepage which is correct, after that, I logged out the account and try to Type https://localhost:44360/Home/Home and unfortunately it was redirected or even clicking the back button of the browser.
I am currently working on the code below.
LoginController.cs
[HttpPost]
public ActionResult Login(LoginModel userInfo, FormCollection collection, string returnUrl)
{
ILogicInterface<LogicalUserInput, LogicalSystemResult> iLogic = new UserLoginCheck();
LogicalUserInput userInput = new LogicalUserInput();
_ = new LogicalSystemResult();
try
{
userInput[typeof(LoginModel).FullName] = userInfo;
LogicalSystemResult systemResult = iLogic.DoProcess(userInput);
bool userCheckExist = systemResult.ResultCode != LogicalSystemResult.RESULT_CODE_ERR_DATA_NOT_EXIST;
if (userCheckExist)
{
UserLoginModel loginModel = systemResult[typeof(UserLoginModel).FullName] as UserLoginModel;
Session["userInfo"] = loginModel;
FormsAuthentication.SetAuthCookie(loginModel.email, true);
if (!string.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
if (loginModel.AccountType == 0) {
return RedirectToAction("Home", "Home");
} else {
return RedirectToAction("Error", "Error");
}
}
}
else
{
TempData.Clear();
TempData["Title"] = "Error!";
TempData["Message"] = " Invalid Username Or Password.";
return View();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return RedirectToAction("Error", "Error");
}
}
Logout Button in HOME.cshtml
<a id="cmdLogOff">
<i class="feather icon-log-out"></i>Logout
</a>
Click Event of LogoutButton
<script>
$("#cmdLogOff").on("click", function () {
$("#HomeView").submit();
});
</script>
#using (Ajax.BeginForm("LogOut",
null,
new AjaxOptions
{
},
new { id = "HomeView" }))
{
}
HOMEController.cs
[Authorize]
public ActionResult Home()
{
if (Session["userInfo"] == null) {
return RedirectToAction("Error", "Error");
} else {
UserLoginModel userLoginModel = Session["userInfo"] as UserLoginModel;
TempData["user"] = userLoginModel.lastName + ", " + userLoginModel.firstName + " " + userLoginModel.middleName;
return View();
}
}
[Authorize]
[HttpPost]
public ActionResult LogOut() {
try {
Session.Abandon();
FormsAuthentication.SignOut();
Session["userInfo"] = null;
return RedirectToAction("Login", "Login");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
return View();
}
}
The main question here is that, how can I not redirect the user if he/she just only type the link he/she wants to access without an account. And after logging out of an account, the Home Page will not be shown even if the user clicked the back of the browser?
This is my method in cshtml:
public async Task<IActionResult> OnGetAsync(string username)
{
try
{
var user = await _userManager.GetUserAsync(User);
username = await _userManager.GetUserNameAsync(user);
await LoadAsync(user);
return Page();
}
catch (Exception)
{
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
}
I added in cshtml at the top the parameter like #page "{username?}"
I want to redirect the user to www.domainname.com/IndexUser?username=usernameparam.
How can I do this? I looked everywhere and can't find a way to do it.
I am working on a MVC Web App which is calling a Web API. In my Create (POST) method, a user will enter email addresses of some users in the database. I have a check to enter the email only if the email does not already exist in the database or not. If it already exists, I want to be able to show an error message to the user "Email already exists".
I don't know how to handle the response to display an error message to the user when it fails to create an approveduser? I am trying with a try-catch case, but it is not working. I have ViewBag.Message = error which I am trying to display in my Index View when it fails. But nothing is displaying. When I debug, it doesn't even go to the catch when the email exists in the database, it just takes me to the Index view.
public ActionResult Create([Bind(Include = "Email,FirstName,LastName")] ApprovedUsers approvedUsers)
{
try
{
using (WebClient client = new WebClient())
{
token = Session.GetDataFromSession<string>("access_token");
client.Headers.Add("authorization", "Bearer " + token);
byte[] response = client.UploadValues(apiUrl, "POST", new NameValueCollection()
{
{ "Email", approvedUsers.Email },
{ "FirstName",approvedUsers.FirstName },
{ "LastName",approvedUsers.LastName }
});
string result = System.Text.Encoding.UTF8.GetString(response);
return RedirectToAction("Index");
}
}
catch
{
return RedirectToAction("Index", new { error = "Email exists" });
}
}
Index Action
public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page, string error)
{
ViewBag.Message = error;
This is the API method being called.
public IHttpActionResult PostApprovedUsers(ApprovedUsers approvedUsers)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (!db.ApprovedUsers.Any(u => u.Email == approvedUsers.Email))
{
db.ApprovedUsers.Add(approvedUsers);
db.SaveChanges();
}
return CreatedAtRoute("DefaultApi", new { id = approvedUsers.Email }, approvedUsers);
Try this:
public ActionResult Create([Bind(Include = "Email,FirstName,LastName")] ApprovedUsers approvedUsers)
{
try
{
using (WebClient client = new WebClient())
{
token = Session.GetDataFromSession<string>("access_token");
client.Headers.Add("authorization", "Bearer " + token);
byte[] response = client.UploadValues(apiUrl, "POST", new NameValueCollection()
{
{ "Email", approvedUsers.Email },
{ "FirstName",approvedUsers.FirstName },
{ "LastName",approvedUsers.LastName }
});
string result = System.Text.Encoding.UTF8.GetString(response);
}
}
catch
{
TempData["Error"] = "Email Exists";});
}
RedirectToAction("Index");
}
public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page, string error)
{
ViewBag.Message = TempData["Error"].ToString();
}
It was my API that was causing the issue. Even when it failed to create the duplicate email, it never went to the catch because I didn't have a bad request return if the email did exist in the database.
Changed my API to this and then it was working.
if (db.ApprovedUsers.Any(u => u.Email == approvedUsers.Email))
{
return Content(HttpStatusCode.BadRequest, "Email exists already");
}
else
{
db.ApprovedUsers.Add(approvedUsers);
}
try
{
db.SaveChanges();
}
catch (DbUpdateException e)
{
if (ApprovedUsersExists(approvedUsers.Id))
{
return Conflict();
}
throw e;
I am trying to enable the email confirmation on my MVC 5 app.
Here is my code as I keep get this error:
[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]
This is my register action:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
First = model.First,
Last = model.Last
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
UserManager.AddToRole(user.Id, "Default");
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here");
return View("DisplayEmail");
//return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
and here is the email section
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
var emailManager = new EmailConfig(EmailConfig.EmailSystem, message.Destination, message.Subject, message.Body);
return emailManager.Send();
// Plug in your email service here to send an email.
}
}
and here is where the email actually gets sent
public Task Send()
{
foreach(var mail in this.mails)
{
this.client.SendMailAsync(mail);
}
return Task.FromResult(0);
}
I have a problem starting a new asp.net application. I choose asp.net mvc template with Individual User Accounts authentication and project generates and starts normally. My question is: what do i have to do to use register/login options? From what I understood methods responsible for these actions are generated automatically, but should I do something about database schema for information about users (shoudn't it be created also automatically?).
Anyway, after filling fields in register form i get error that a file couldn't be found and the error is found on line 155: var result = await UserManager.CreateAsync(user, model.Password);
I'm using Visual Studio Community 2015 on windows 8. Thank you in advance
public async Task<IActionResult> Register(RegisterVM registerVM)
{
if (!ModelState.IsValid)
{
return View();
}
AppUser appUser = new AppUser()
{
Fullname = registerVM.Fullname,
UserName = registerVM.Username,
Email = registerVM.Email
};
IdentityResult result = await _userManager.CreateAsync(appUser, registerVM.Password);
if (!result.Succeeded)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
return View(registerVM);
}
await _userManager.AddToRoleAsync(appUser, Roless.Admin.ToString());
await _signInManager.SignInAsync(appUser, true);
return RedirectToAction("index", "home");
}
public IActionResult Login()
{
return View();
}
[HttpPost]
[AutoValidateAntiforgeryToken]
public async Task<IActionResult> Login(LoginVM loginVM, string returnurl)
{
if (!ModelState.IsValid)
{
return View();
}
AppUser dbUser = await _userManager.FindByEmailAsync(loginVM.Email);
if (dbUser == null)
{
ModelState.AddModelError("", "Ya email ya da Password sehvdir");
return View(loginVM);
}
SignInResult result = await _signInManager.PasswordSignInAsync(dbUser, loginVM.Password, loginVM.RememerMe, true);
if (result.IsLockedOut)
{
ModelState.AddModelError("", "Your Account Is Lock Out");
return View(loginVM);
}
if (!result.Succeeded)
{
ModelState.AddModelError("", "Ya Email ya da Password sehvdir");
return View(loginVM);
}
if (returnurl == null)
{
return RedirectToAction("index", "home");
}
foreach (var item in await _userManager.GetRolesAsync(dbUser))
{
if (item.Contains(Roless.Admin.ToString()))
{
return RedirectToAction("index", "Dashboard", new { area = "AdminF" });
}
}
return Redirect(returnurl);
}