去掉Owin的一些代码

This commit is contained in:
yubaolee
2015-09-22 23:10:00 +08:00
parent 2f9b41b96d
commit 2a5cdd453f
18 changed files with 1254 additions and 698 deletions

View File

@@ -1,122 +1,95 @@
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
}
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 Infrastructure.Helper;
using Newtonsoft.Json;
using OpenAuth.App;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
namespace OpenAuth.Mvc.Controllers
{
[Authorize]
public class AccountController : Controller
{
private LoginApp _loginApp;
private IUserRepository _userRepository;
public AccountController(IUserRepository repository)
{
_userRepository = repository;
_loginApp = new LoginApp(_userRepository);
}
//
// 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)
{
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// 如果我们进行到这一步时某个地方出错,则重新显示表单
return View(model);
}
//
// POST: /Account/LogOff
public ActionResult LogOff()
{
SessionHelper.Clear();
return RedirectToAction("Login", "Account");
}
public ActionResult List()
{
return View();
}
public string LoadUsers()
{
return JsonConvert.SerializeObject(_userRepository.LoadUsers());
}
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
}

View File

@@ -0,0 +1,35 @@
// ***********************************************************************
// Assembly : OpenAuth.Mvc
// Author : Administrator
// Created : 09-22-2015
//
// Last Modified By : Administrator
// Last Modified On : 09-22-2015
// ***********************************************************************
// <copyright file="BaseController.cs" company="">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary>基础控制器,设置权限</summary>
// ***********************************************************************
using System.Web.Mvc;
using Infrastructure.Helper;
using OpenAuth.Domain;
namespace OpenAuth.Mvc.Controllers
{
public class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
#region Session过期自动跳出登录画面
if (SessionHelper.GetSessionUser<User>() == null)
{
Response.Redirect("~/Account/Login");
}
#endregion
}
}
}

View File

@@ -1,17 +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();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace OpenAuth.Mvc.Controllers
{
public class HomeController : BaseController
{
public ActionResult Index()
{
return View();
}
}
}