Skip to content

Commit a20c38d

Browse files
committed
Initial commit
1 parent a0d2de6 commit a20c38d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+29342
-0
lines changed

MVC5Course.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.31101.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVC5Course", "MVC5Course\MVC5Course.csproj", "{F05FD365-217D-40BA-B3E2-8FFA108254A0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F05FD365-217D-40BA-B3E2-8FFA108254A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F05FD365-217D-40BA-B3E2-8FFA108254A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F05FD365-217D-40BA-B3E2-8FFA108254A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F05FD365-217D-40BA-B3E2-8FFA108254A0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

MVC5Course/App_Start/BundleConfig.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Web;
2+
using System.Web.Optimization;
3+
4+
namespace MVC5Course
5+
{
6+
public class BundleConfig
7+
{
8+
// 如需「搭配」的詳細資訊,請瀏覽 http://go.microsoft.com/fwlink/?LinkId=301862
9+
public static void RegisterBundles(BundleCollection bundles)
10+
{
11+
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
12+
"~/Scripts/jquery-{version}.js"));
13+
14+
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
15+
"~/Scripts/jquery.validate*"));
16+
17+
// 使用開發版本的 Modernizr 進行開發並學習。然後,當您
18+
// 準備好實際執行時,請使用 http://modernizr.com 上的建置工具,只選擇您需要的測試。
19+
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
20+
"~/Scripts/modernizr-*"));
21+
22+
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
23+
"~/Scripts/bootstrap.js",
24+
"~/Scripts/respond.js"));
25+
26+
bundles.Add(new StyleBundle("~/Content/css").Include(
27+
"~/Content/bootstrap.css",
28+
"~/Content/site.css"));
29+
}
30+
}
31+
}

MVC5Course/App_Start/FilterConfig.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Web;
2+
using System.Web.Mvc;
3+
4+
namespace MVC5Course
5+
{
6+
public class FilterConfig
7+
{
8+
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
9+
{
10+
filters.Add(new HandleErrorAttribute());
11+
}
12+
}
13+
}
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity;
4+
using System.Linq;
5+
using System.Security.Claims;
6+
using System.Threading.Tasks;
7+
using System.Web;
8+
using Microsoft.AspNet.Identity;
9+
using Microsoft.AspNet.Identity.EntityFramework;
10+
using Microsoft.AspNet.Identity.Owin;
11+
using Microsoft.Owin;
12+
using Microsoft.Owin.Security;
13+
using MVC5Course.Models;
14+
15+
namespace MVC5Course
16+
{
17+
public class EmailService : IIdentityMessageService
18+
{
19+
public Task SendAsync(IdentityMessage message)
20+
{
21+
// 將您的電子郵件服務外掛到這裡以傳送電子郵件。
22+
return Task.FromResult(0);
23+
}
24+
}
25+
26+
public class SmsService : IIdentityMessageService
27+
{
28+
public Task SendAsync(IdentityMessage message)
29+
{
30+
// 將您的 SMS 服務外掛到這裡以傳送簡訊。
31+
return Task.FromResult(0);
32+
}
33+
}
34+
35+
// 設定此應用程式中使用的應用程式使用者管理員。UserManager 在 ASP.NET Identity 中定義且由應用程式中使用。
36+
public class ApplicationUserManager : UserManager<ApplicationUser>
37+
{
38+
public ApplicationUserManager(IUserStore<ApplicationUser> store)
39+
: base(store)
40+
{
41+
}
42+
43+
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
44+
{
45+
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
46+
// 設定使用者名稱的驗證邏輯
47+
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
48+
{
49+
AllowOnlyAlphanumericUserNames = false,
50+
RequireUniqueEmail = true
51+
};
52+
53+
// 設定密碼的驗證邏輯
54+
manager.PasswordValidator = new PasswordValidator
55+
{
56+
RequiredLength = 6,
57+
RequireNonLetterOrDigit = true,
58+
RequireDigit = true,
59+
RequireLowercase = true,
60+
RequireUppercase = true,
61+
};
62+
63+
// 設定使用者鎖定詳細資料
64+
manager.UserLockoutEnabledByDefault = true;
65+
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
66+
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
67+
68+
// 註冊雙因素驗證提供者。此應用程式使用手機和電子郵件接收驗證碼以驗證使用者
69+
// 您可以撰寫專屬提供者,並將它外掛到這裡。
70+
manager.RegisterTwoFactorProvider("電話代碼", new PhoneNumberTokenProvider<ApplicationUser>
71+
{
72+
MessageFormat = "您的安全碼為 {0}"
73+
});
74+
manager.RegisterTwoFactorProvider("電子郵件代碼", new EmailTokenProvider<ApplicationUser>
75+
{
76+
Subject = "安全碼",
77+
BodyFormat = "您的安全碼為 {0}"
78+
});
79+
manager.EmailService = new EmailService();
80+
manager.SmsService = new SmsService();
81+
var dataProtectionProvider = options.DataProtectionProvider;
82+
if (dataProtectionProvider != null)
83+
{
84+
manager.UserTokenProvider =
85+
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
86+
}
87+
return manager;
88+
}
89+
}
90+
91+
// 設定在此應用程式中使用的應用程式登入管理員。
92+
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
93+
{
94+
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
95+
: base(userManager, authenticationManager)
96+
{
97+
}
98+
99+
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
100+
{
101+
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
102+
}
103+
104+
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
105+
{
106+
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
107+
}
108+
}
109+
}

MVC5Course/App_Start/RouteConfig.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.Mvc;
6+
using System.Web.Routing;
7+
8+
namespace MVC5Course
9+
{
10+
public class RouteConfig
11+
{
12+
public static void RegisterRoutes(RouteCollection routes)
13+
{
14+
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
15+
16+
routes.MapRoute(
17+
name: "Default",
18+
url: "{controller}/{action}/{id}",
19+
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
20+
);
21+
}
22+
}
23+
}

MVC5Course/App_Start/Startup.Auth.cs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using Microsoft.AspNet.Identity;
3+
using Microsoft.AspNet.Identity.Owin;
4+
using Microsoft.Owin;
5+
using Microsoft.Owin.Security.Cookies;
6+
using Microsoft.Owin.Security.Google;
7+
using Owin;
8+
using MVC5Course.Models;
9+
10+
namespace MVC5Course
11+
{
12+
public partial class Startup
13+
{
14+
// 如需設定驗證的詳細資訊,請瀏覽 http://go.microsoft.com/fwlink/?LinkId=301864
15+
public void ConfigureAuth(IAppBuilder app)
16+
{
17+
// 設定資料庫內容、使用者管理員和登入管理員,以針對每個要求使用單一執行個體
18+
app.CreatePerOwinContext(ApplicationDbContext.Create);
19+
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
20+
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
21+
22+
// 讓應用程式使用 Cookie 儲存已登入使用者的資訊
23+
// 並使用 Cookie 暫時儲存使用者利用協力廠商登入提供者登入的相關資訊;
24+
// 在 Cookie 中設定簽章
25+
app.UseCookieAuthentication(new CookieAuthenticationOptions
26+
{
27+
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
28+
LoginPath = new PathString("/Account/Login"),
29+
Provider = new CookieAuthenticationProvider
30+
{
31+
// 讓應用程式在使用者登入時驗證安全性戳記。
32+
// 這是您變更密碼或將外部登入新增至帳戶時所使用的安全性功能。
33+
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
34+
validateInterval: TimeSpan.FromMinutes(30),
35+
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
36+
}
37+
});
38+
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
39+
40+
// 讓應用程式在雙因素驗證程序中驗證第二個因素時暫時儲存使用者資訊。
41+
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
42+
43+
// 讓應用程式記住第二個登入驗證因素 (例如電話或電子郵件)。
44+
// 核取此選項之後,將會在用來登入的裝置上記住登入程序期間的第二個驗證步驟。
45+
// 這類似於登入時的 RememberMe 選項。
46+
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
47+
48+
// 註銷下列各行以啟用利用協力廠商登入提供者登入
49+
//app.UseMicrosoftAccountAuthentication(
50+
// clientId: "",
51+
// clientSecret: "");
52+
53+
//app.UseTwitterAuthentication(
54+
// consumerKey: "",
55+
// consumerSecret: "");
56+
57+
//app.UseFacebookAuthentication(
58+
// appId: "",
59+
// appSecret: "");
60+
61+
//app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
62+
//{
63+
// ClientId = "",
64+
// ClientSecret = ""
65+
//});
66+
}
67+
}
68+
}

MVC5Course/App_Start/WebApiConfig.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web.Http;
5+
6+
namespace MVC5Course
7+
{
8+
public static class WebApiConfig
9+
{
10+
public static void Register(HttpConfiguration config)
11+
{
12+
// Web API 設定和服務
13+
14+
// Web API 路由
15+
config.MapHttpAttributeRoutes();
16+
17+
config.Routes.MapHttpRoute(
18+
name: "DefaultApi",
19+
routeTemplate: "api/{controller}/{id}",
20+
defaults: new { id = RouteParameter.Optional }
21+
);
22+
}
23+
}
24+
}

MVC5Course/Content/Site.css

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
body {
2+
padding-top: 50px;
3+
padding-bottom: 20px;
4+
}
5+
6+
/* Set padding to keep content from hitting the edges */
7+
.body-content {
8+
padding-left: 15px;
9+
padding-right: 15px;
10+
}
11+
12+
/* Override the default bootstrap behavior where horizontal description lists
13+
will truncate terms that are too long to fit in the left column
14+
*/
15+
.dl-horizontal dt {
16+
white-space: normal;
17+
}
18+
19+
/* Set width on the form input elements since they're 100% wide by default */
20+
input,
21+
select,
22+
textarea {
23+
max-width: 280px;
24+
}

0 commit comments

Comments
 (0)