|
|
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.EntityFramework;
- using Microsoft.Owin.Security;
- using Sevomin.Models;
- using System;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.Mvc;
-
- namespace Sevomin.WebFrontend.Controllers
- {
- public class AccountController : BaseController
- {
- public AccountController()
- : this(new UserManager<User>(new UserStore<User>(SevominDbContext.Current)))
- {
- }
-
- public AccountController(UserManager<User> userManager)
- {
- UserManager = userManager;
- UserManager.UserValidator = new Sevomin.Models.Helpers.SevominUserValidator();
- }
-
- public UserManager<User> UserManager { get; private set; }
-
- private IAuthenticationManager AuthenticationManager
- {
- get
- {
- return HttpContext.GetOwinContext().Authentication;
- }
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Signup(SignupViewModel model)
- {
- User user;
- if (model.IsAvalin)
- {
- user = new Avalin(model.Email, model.DisplayName);
- user.SignUpDate = DateTime.UtcNow;
- }
- else
- {
- int spaceIndex = model.DisplayName.IndexOf(' ');
- user = new Dovomin(model.Email, model.DisplayName, string.Empty);
- user.SignUpDate = DateTime.UtcNow;
- }
- var result = await UserManager.CreateAsync(user, model.Password);
- if (result.Succeeded)
- {
- await SignInAsync(user, isPersistent: false);
- return RedirectToAction("Index", "Home");
- }
- else
- {
- AddErrors(result);
- }
-
- return View("Intro", model);
- }
-
- public async Task<ActionResult> CheckUsername(string Email)
- {
- bool result = (await UserManager.FindByNameAsync(Email)) == null;
- if(result)
- return Json(true, JsonRequestBehavior.AllowGet);
-
- return Json("این ایمیل قبلا در سایت استفاده شده. کلمه عبور خود را فراموش کرده اید؟", JsonRequestBehavior.AllowGet);
- }
-
-
- public ActionResult Login(string returnUrl)
- {
- if(Request.IsAuthenticated)
- return RedirectToAction("Index", "Home");
-
- ViewBag.ReturnUrl = returnUrl;
- return View();
- }
-
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
- {
- if (ModelState.IsValid)
- {
- var user = await UserManager.FindAsync(model.Username, model.Password);
- if (user != null)
- {
- await SignInAsync(user, true);
- return RedirectToLocal(returnUrl);
- }
- else
- {
- ModelState.AddModelError("", "نام کاربری و یا کلمه عبور وارد شده صحیح نمی باشد.");
- }
- }
-
- // If we got this far, something failed, redisplay form
- return View(model);
- }
-
-
- public ActionResult Logout()
- {
- AuthenticationManager.SignOut();
- return RedirectToAction("Index", "Home");
- }
-
-
- private async Task SignInAsync(User user, bool isPersistent)
- {
- AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
- var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
- AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
- }
-
- private ActionResult RedirectToLocal(string returnUrl)
- {
- if (Url.IsLocalUrl(returnUrl))
- {
- return Redirect(returnUrl);
- }
- else
- {
- return RedirectToAction("Index", "Home");
- }
- }
-
- private void AddErrors(IdentityResult result)
- {
- foreach (var error in result.Errors)
- {
- ModelState.AddModelError("", error);
- }
- }
- }
- }
|