NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using BackEndProject.Models;
using BackEndProject.ViewModels;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using MimeKit;
using MimeKit.Text;
using System.IO;
using BackEndProject.Services.Interfaces;
using BackEndProject.ViewModels.AccountVM;

namespace BackEndProject.Controllers
{
public class AccountController : Controller
{
private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager;
private readonly IEmailService _emailService;
private readonly IFileService _fileService;


public AccountController(UserManager<AppUser> userManager,
SignInManager<AppUser> signInManager,
IEmailService emailService,
IFileService fileService)
{
_userManager = userManager;
_signInManager = signInManager;
_emailService = emailService;
_fileService = fileService;
}

[HttpGet]
public IActionResult Register()
{
return View();
}


[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterVM registerVM)
{
if (!ModelState.IsValid) return View(registerVM);

AppUser user = new AppUser
{
FullName = registerVM.FullName,
UserName = registerVM.Username,
Email = registerVM.Email
};

IdentityResult result = await _userManager.CreateAsync(user, registerVM.Password);

if (!result.Succeeded)
{
foreach (var item in result.Errors)
{
ModelState.AddModelError("", item.Description);
}
return View(registerVM);
}

string token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

string link = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, token },
Request.Scheme, Request.Host.ToString());

string body = string.Empty;

string path = "wwwroot/Template/Verify.html";
body = _fileService.ReadFile(path, body);


body = body.Replace("{{link}}", link);
body = body.Replace("{{Fullname}}", user.FullName);


string subject = "Verify Email";
_emailService.Send(user.Email, subject, body);

return RedirectToAction(nameof(VerifyEmail));
}

public async Task<IActionResult> ConfirmEmail(string userId, string token)
{
if (userId == null || token == null) return NotFound();

AppUser user = await _userManager.FindByIdAsync(userId);

if (user == null) return NotFound();

await _userManager.ConfirmEmailAsync(user, token);

await _signInManager.SignInAsync(user, false);

return RedirectToAction(nameof(Login));
}

public IActionResult VerifyEmail()
{
return View();
}

public IActionResult ForgotPassword()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ForgotPassword(ForgotPasswordVM forgotPassword)
{
if (!ModelState.IsValid) return NotFound();

AppUser exsistUser = await _userManager.FindByEmailAsync(forgotPassword.Email);

if (exsistUser == null)
{
ModelState.AddModelError("Email", "Email isn't found");
return View();
}

string token = await _userManager.GeneratePasswordResetTokenAsync(exsistUser);

string link = Url.Action(nameof(ResetPassword), "Account", new { userId = exsistUser.Id, token },
Request.Scheme, Request.Host.ToString());



string body = string.Empty;

string path = "wwwroot/Template/Verify.html";
body = _fileService.ReadFile(path, body);


body = body.Replace("{{link}}", link);
body = body.Replace("{{Fullname}}", exsistUser.FullName);


string subject = "Verify Email";
_emailService.Send(exsistUser.Email, subject, body);

return RedirectToAction(nameof(VerifyEmail));

}

public async Task<IActionResult> ResetPassword(string userId, string token)
{
ResetPasswordVM resetPassword = new ResetPasswordVM()
{
UserId = userId,
Token = token
};
return View(resetPassword);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ResetPassword(ResetPasswordVM resetPassword)
{
if (!ModelState.IsValid) return View();



AppUser exsistUser = await _userManager.FindByIdAsync(resetPassword.UserId);


bool chekPassword = await _userManager.VerifyUserTokenAsync(exsistUser, _userManager.Options.Tokens.PasswordResetTokenProvider, "ResetPassword", resetPassword.Token);

if (!chekPassword) return Content("Error");


if (exsistUser == null) return NotFound();

if (await _userManager.CheckPasswordAsync(exsistUser, resetPassword.Password))
{
ModelState.AddModelError("", "This password is your last password");
return View(resetPassword);
}



await _userManager.ResetPasswordAsync(exsistUser, resetPassword.Token, resetPassword.Password);

await _userManager.UpdateSecurityStampAsync(exsistUser);

return RedirectToAction(nameof(Login));
}

[HttpGet]
public IActionResult Login()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginVM loginVM)
{
if (!ModelState.IsValid)
{
return View(loginVM);
}

AppUser user = await _userManager.FindByEmailAsync(loginVM.UsernameorEmail);

if (user == null)
{
user = await _userManager.FindByNameAsync(loginVM.UsernameorEmail);
}

if (user == null)
{
ModelState.AddModelError("", "Email or password wrong");
return View(loginVM);
}

var result = await _signInManager.PasswordSignInAsync(user, loginVM.Password, false, false);

if (!result.Succeeded)
{
ModelState.AddModelError("", "Email or password wrong!");
return View(loginVM);
}

return RedirectToAction("index", "Home");
}

public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();

return RedirectToAction("Index", "Home");
}

}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.