添加DI支持

This commit is contained in:
yubaolee 2015-09-23 00:10:11 +08:00
parent 2a5cdd453f
commit a284f975c3
10 changed files with 383 additions and 363 deletions

View File

@ -0,0 +1,50 @@
// ***********************************************************************
// Assembly : Infrastructure
// Author : Administrator
// Created : 09-22-2015
//
// Last Modified By : Administrator
// Last Modified On : 09-22-2015
// ***********************************************************************
// <copyright file="SessionHelper.cs" company="">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary>SESSION辅助类</summary>
// ***********************************************************************
using System;
using System.Web;
namespace Infrastructure.Helper
{
/// <summary>
/// Session 帮助类
/// </summary>
public class SessionHelper
{
private static readonly string SessionUser = "SESSION_USER";
public static void AddSessionUser<T>(T user)
{
HttpContext rq = HttpContext.Current;
rq.Session[SessionUser] = user;
}
public static T GetSessionUser<T>()
{
try
{
HttpContext rq = HttpContext.Current;
return (T)rq.Session[SessionUser];
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static void Clear()
{
HttpContext rq = HttpContext.Current;
rq.Session[SessionUser] = null;
}
}
}

View File

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
namespace OpenAuth.App
@ -24,5 +26,10 @@ namespace OpenAuth.App
}
public IEnumerable<User> LoadUsers()
{
return _repository.LoadUsers();
}
}
}

View File

@ -1,16 +1,11 @@
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using OpenAuth.Mvc.Models;
using System.Collections.Generic;
using System.Security.Claims;
using System;
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;
using OpenAuth.Mvc.Models;
namespace OpenAuth.Mvc.Controllers
{
@ -18,12 +13,10 @@ namespace OpenAuth.Mvc.Controllers
public class AccountController : Controller
{
private LoginApp _loginApp;
private IUserRepository _userRepository;
public AccountController(IUserRepository repository)
public AccountController()
{
_userRepository = repository;
_loginApp = new LoginApp(_userRepository);
_loginApp = (LoginApp) DependencyResolver.Current.GetService(typeof (LoginApp));
}
//
// GET: /Account/Login
@ -44,15 +37,15 @@ namespace OpenAuth.Mvc.Controllers
if (ModelState.IsValid)
{
//直接生成登陆用户,在实际的项目中采用数据库形式
var user = new User {Account = "admin"};
if (user != null)
try
{
_loginApp.Login(model.UserName, model.Password);
SessionHelper.AddSessionUser(model);
return RedirectToLocal(returnUrl);
}
else
catch (Exception exception)
{
ModelState.AddModelError("", "Invalid username or password.");
ModelState.AddModelError("", exception.Message);
}
}
@ -72,10 +65,7 @@ namespace OpenAuth.Mvc.Controllers
{
return View();
}
public string LoadUsers()
{
return JsonConvert.SerializeObject(_userRepository.LoadUsers());
}

View File

@ -15,6 +15,7 @@
using System.Web.Mvc;
using Infrastructure.Helper;
using OpenAuth.Domain;
using OpenAuth.Mvc.Models;
namespace OpenAuth.Mvc.Controllers
{
@ -25,7 +26,7 @@ namespace OpenAuth.Mvc.Controllers
base.OnActionExecuting(filterContext);
#region Session过期自动跳出登录画面
if (SessionHelper.GetSessionUser<User>() == null)
if (SessionHelper.GetSessionUser<LoginViewModel>() == null)
{
Response.Redirect("~/Account/Login");
}

View File

@ -3,15 +3,28 @@ using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using OpenAuth.App;
namespace OpenAuth.Mvc.Controllers
{
public class HomeController : BaseController
{
private LoginApp _loginApp;
public HomeController()
{
_loginApp = (LoginApp)DependencyResolver.Current.GetService(typeof(LoginApp));
}
public ActionResult Index()
{
return View();
}
public string LoadUsers()
{
return JsonConvert.SerializeObject(_loginApp.LoadUsers());
}
}
}

View File

@ -8,6 +8,7 @@ using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using OpenAuth.App;
using OpenAuth.Domain.Interface;
using OpenAuth.Repository;
@ -31,6 +32,7 @@ namespace OpenAuth.Mvc
var builder = new ContainerBuilder();
builder.RegisterType<UserRepository>().As<IUserRepository>();
builder.RegisterType<LoginApp>();
// Register your MVC controllers.
builder.RegisterControllers(typeof (MvcApplication).Assembly);

View File

@ -17,8 +17,8 @@
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressAnonymousAuthentication>enabled</IISExpressAnonymousAuthentication>
<IISExpressWindowsAuthentication>disabled</IISExpressWindowsAuthentication>
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@ -107,10 +107,6 @@
<Private>True</Private>
<HintPath>..\packages\WebGrease.1.5.2\lib\WebGrease.dll</HintPath>
</Reference>
<Reference Include="Antlr3.Runtime">
<Private>True</Private>
<HintPath>..\packages\Antlr.3.4.1.9004\lib\Antlr3.Runtime.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Reference Include="EntityFramework">
@ -119,45 +115,6 @@
<Reference Include="EntityFramework.SqlServer">
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.Identity.Core">
<HintPath>..\packages\Microsoft.AspNet.Identity.Core.1.0.0\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.Identity.Owin">
<HintPath>..\packages\Microsoft.AspNet.Identity.Owin.1.0.0\lib\net45\Microsoft.AspNet.Identity.Owin.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.Identity.EntityFramework">
<HintPath>..\packages\Microsoft.AspNet.Identity.EntityFramework.1.0.0\lib\net45\Microsoft.AspNet.Identity.EntityFramework.dll</HintPath>
</Reference>
<Reference Include="Owin">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin">
<HintPath>..\packages\Microsoft.Owin.2.0.0\lib\net45\Microsoft.Owin.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Host.SystemWeb">
<HintPath>..\packages\Microsoft.Owin.Host.SystemWeb.2.0.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security">
<HintPath>..\packages\Microsoft.Owin.Security.2.0.0\lib\net45\Microsoft.Owin.Security.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security.Facebook">
<HintPath>..\packages\Microsoft.Owin.Security.Facebook.2.0.0\lib\net45\Microsoft.Owin.Security.Facebook.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security.Cookies">
<HintPath>..\packages\Microsoft.Owin.Security.Cookies.2.0.0\lib\net45\Microsoft.Owin.Security.Cookies.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security.OAuth">
<HintPath>..\packages\Microsoft.Owin.Security.OAuth.2.0.0\lib\net45\Microsoft.Owin.Security.OAuth.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security.Google">
<HintPath>..\packages\Microsoft.Owin.Security.Google.2.0.0\lib\net45\Microsoft.Owin.Security.Google.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security.Twitter">
<HintPath>..\packages\Microsoft.Owin.Security.Twitter.2.0.0\lib\net45\Microsoft.Owin.Security.Twitter.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security.MicrosoftAccount">
<HintPath>..\packages\Microsoft.Owin.Security.MicrosoftAccount.2.0.0\lib\net45\Microsoft.Owin.Security.MicrosoftAccount.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\BundleConfig.cs" />

View File

@ -46,7 +46,7 @@
var grid_selector = "#grid-table";
var pager_selector = "#grid-pager";
jQuery(grid_selector).jqGrid({
url: "/Account/LoadUsers",
url: "/Home/LoadUsers",
mtype: "post",
datatype: "json",
height: '100%',