mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-21 02:57:54 +08:00
调整了一下项目框架与前端JS代码
This commit is contained in:
122
OpenAuth.Mvc/Controllers/AccountController.cs
Normal file
122
OpenAuth.Mvc/Controllers/AccountController.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.Owin.Security;
|
||||
using OpenAuth.Mvc.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using OpenAuth.App;
|
||||
using OpenAuth.Domain;
|
||||
using OpenAuth.Domain.Interface;
|
||||
|
||||
namespace OpenAuth.Mvc.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
public class AccountController : Controller
|
||||
{
|
||||
private IUserRepository _userRepository;
|
||||
|
||||
public AccountController(IUserRepository repository)
|
||||
{
|
||||
_userRepository = repository;
|
||||
}
|
||||
//
|
||||
// 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)
|
||||
{
|
||||
//直接生成登陆用户,在实际的项目中采用数据库形式
|
||||
var user = new User {Account = "admin"};
|
||||
if (user != null)
|
||||
{
|
||||
await SignInAsync(user, model.RememberMe);
|
||||
return RedirectToLocal(returnUrl);
|
||||
}
|
||||
else
|
||||
{
|
||||
ModelState.AddModelError("", "Invalid username or password.");
|
||||
}
|
||||
}
|
||||
|
||||
// 如果我们进行到这一步时某个地方出错,则重新显示表单
|
||||
return View(model);
|
||||
}
|
||||
|
||||
//
|
||||
// POST: /Account/LogOff
|
||||
public ActionResult LogOff()
|
||||
{
|
||||
AuthenticationManager.SignOut();
|
||||
return RedirectToAction("Login", "Account");
|
||||
}
|
||||
|
||||
public ActionResult List()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
public string LoadUsers()
|
||||
{
|
||||
return JsonConvert.SerializeObject(_userRepository.LoadUsers());
|
||||
}
|
||||
|
||||
#region 帮助程序
|
||||
|
||||
private IAuthenticationManager AuthenticationManager
|
||||
{
|
||||
get
|
||||
{
|
||||
return HttpContext.GetOwinContext().Authentication;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// sign information as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="user">用户</param>
|
||||
/// <param name="isPersistent">Remember me?</param>
|
||||
/// <returns>Task.</returns>
|
||||
private async Task SignInAsync(User user, bool isPersistent)
|
||||
{
|
||||
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.Name, user.Account),
|
||||
new Claim(ClaimTypes.Role, "Administrator"),
|
||||
new Claim(ClaimTypes.NameIdentifier, "7c301fe4-099e-46f9-bdb8-e922d73a8031"),
|
||||
new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider",
|
||||
"ASP.NET Identity")
|
||||
};
|
||||
var identity = new ClaimsIdentity(claims, 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");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 帮助程序
|
||||
}
|
||||
}
|
17
OpenAuth.Mvc/Controllers/HomeController.cs
Normal file
17
OpenAuth.Mvc/Controllers/HomeController.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace OpenAuth.Mvc.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
public ActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user