OpenAuth.Net/OpenAuth.Mvc/Controllers/AccountController.cs

85 lines
2.2 KiB
C#
Raw Normal View History

2015-09-23 00:10:11 +08:00
using System;
2015-09-22 23:10:00 +08:00
using System.Threading.Tasks;
using System.Web.Mvc;
using Infrastructure.Helper;
using Newtonsoft.Json;
using OpenAuth.App;
using OpenAuth.Domain.Interface;
2015-09-23 00:10:11 +08:00
using OpenAuth.Mvc.Models;
2015-09-22 23:10:00 +08:00
namespace OpenAuth.Mvc.Controllers
{
[Authorize]
public class AccountController : Controller
{
private LoginApp _loginApp;
2015-09-23 00:10:11 +08:00
public AccountController()
2015-09-22 23:10:00 +08:00
{
2015-09-23 00:10:11 +08:00
_loginApp = (LoginApp) DependencyResolver.Current.GetService(typeof (LoginApp));
2015-09-22 23:10:00 +08:00
}
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
//直接生成登陆用户,在实际的项目中采用数据库形式
2015-09-23 00:10:11 +08:00
try
2015-09-22 23:10:00 +08:00
{
2015-09-23 00:10:11 +08:00
_loginApp.Login(model.UserName, model.Password);
SessionHelper.AddSessionUser(model);
2015-09-22 23:10:00 +08:00
return RedirectToLocal(returnUrl);
}
2015-09-23 00:10:11 +08:00
catch (Exception exception)
2015-09-22 23:10:00 +08:00
{
2015-09-23 00:10:11 +08:00
ModelState.AddModelError("", exception.Message);
2015-09-22 23:10:00 +08:00
}
}
// 如果我们进行到这一步时某个地方出错,则重新显示表单
return View(model);
}
//
// POST: /Account/LogOff
public ActionResult LogOff()
{
SessionHelper.Clear();
return RedirectToAction("Login", "Account");
}
public ActionResult List()
{
return View();
}
2015-09-23 00:10:11 +08:00
2015-09-22 23:10:00 +08:00
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
}