├── AngularWebAPI.WebAPI ├── Global.asax ├── Models │ └── Authentication │ │ └── AuthenticateViewModel.cs ├── Global.asax.cs ├── packages.config ├── Controllers │ ├── SecureDataController.cs │ └── AuthenticationController.cs ├── App_Start │ └── WebApiConfig.cs ├── Web.Debug.config ├── Web.Release.config ├── Properties │ └── AssemblyInfo.cs ├── Modules │ └── BasicAuthHttpModule.cs ├── Web.config └── AngularWebAPI.WebAPI.csproj ├── AngularWebAPI.FrontEnd ├── modules │ ├── home │ │ ├── views │ │ │ └── home.html │ │ ├── controllers.js │ │ └── services.js │ └── authentication │ │ ├── controllers.js │ │ ├── views │ │ └── login.html │ │ └── services.js ├── index.html └── scripts │ └── app.js ├── README.md ├── LICENSE ├── AngularWebAPI.sln └── .gitignore /AngularWebAPI.WebAPI/Global.asax: -------------------------------------------------------------------------------- 1 | <%@ Application Codebehind="Global.asax.cs" Inherits="AngularWebAPI.WebAPI.WebApiApplication" Language="C#" %> 2 | -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/modules/home/views/home.html: -------------------------------------------------------------------------------- 1 |

Home

2 |

You're logged in!!

3 |

Secure data from web api: {{secureData}}

4 |

Logout

-------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | angular-webapi-authentication-example 2 | ===================================== 3 | 4 | AngularJS + ASP.NET Web API 2 - Basic HTTP Authentication Example 5 | 6 | For a description and full details go to http://jasonwatmore.com/post/2014/12/01/Web-API-2-Basic-HTTP-Authentication-Example.aspx -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/modules/home/controllers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | angular.module('Home') 4 | 5 | .controller('HomeController', 6 | ['$scope', 'HomeService', 7 | function ($scope, HomeService) { 8 | HomeService.GetSecureData(function (response) { 9 | $scope.secureData = response.secureData; 10 | }); 11 | }]); -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Models/Authentication/AuthenticateViewModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | using System.Web; 6 | 7 | namespace AngularWebAPI.WebAPI.Models.Authentication 8 | { 9 | public class AuthenticateViewModel 10 | { 11 | public string Username { get; set; } 12 | public string Password { get; set; } 13 | } 14 | } -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Global.asax.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web; 5 | using System.Web.Http; 6 | using System.Web.Routing; 7 | 8 | namespace AngularWebAPI.WebAPI 9 | { 10 | public class WebApiApplication : System.Web.HttpApplication 11 | { 12 | protected void Application_Start() 13 | { 14 | GlobalConfiguration.Configure(WebApiConfig.Register); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/modules/home/services.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | angular.module('Home') 4 | 5 | .factory('HomeService', 6 | ['$http', 7 | function ($http) { 8 | var service = {}; 9 | 10 | service.GetSecureData = function (callback) { 11 | $http.get('/api/securedata') 12 | .success(function (response) { 13 | callback(response); 14 | }); 15 | }; 16 | 17 | return service; 18 | }]); -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Controllers/SecureDataController.cs: -------------------------------------------------------------------------------- 1 | using AngularWebAPI.WebAPI.Models.Authentication; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Net; 6 | using System.Net.Http; 7 | using System.Web.Http; 8 | 9 | namespace AngularWebAPI.WebAPI.Controllers 10 | { 11 | [Authorize] 12 | public class SecureDataController : ApiController 13 | { 14 | public IHttpActionResult Get() 15 | { 16 | return Ok(new { secureData = "You have to be authenticated to access this!" }); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/App_Start/WebApiConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Web.Http; 5 | 6 | namespace AngularWebAPI.WebAPI 7 | { 8 | public static class WebApiConfig 9 | { 10 | public static void Register(HttpConfiguration config) 11 | { 12 | // Web API configuration and services 13 | 14 | // Web API routes 15 | config.MapHttpAttributeRoutes(); 16 | 17 | config.Routes.MapHttpRoute( 18 | name: "DefaultApi", 19 | routeTemplate: "{controller}/{id}", 20 | defaults: new { id = RouteParameter.Optional } 21 | ); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/modules/authentication/controllers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | angular.module('Authentication') 4 | 5 | .controller('LoginController', 6 | ['$scope', '$rootScope', '$location', 'AuthenticationService', 7 | function ($scope, $rootScope, $location, AuthenticationService) { 8 | // reset login status 9 | AuthenticationService.ClearCredentials(); 10 | 11 | $scope.login = function () { 12 | $scope.dataLoading = true; 13 | AuthenticationService.Login($scope.username, $scope.password, function (response) { 14 | if (response.success) { 15 | AuthenticationService.SetCredentials($scope.username, $scope.password); 16 | $location.path('/'); 17 | } else { 18 | $scope.error = response.message; 19 | $scope.dataLoading = false; 20 | } 21 | }); 22 | }; 23 | }]); -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Controllers/AuthenticationController.cs: -------------------------------------------------------------------------------- 1 | using AngularWebAPI.WebAPI.Models.Authentication; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Net; 6 | using System.Net.Http; 7 | using System.Web.Http; 8 | 9 | namespace AngularWebAPI.WebAPI.Controllers 10 | { 11 | public class AuthenticationController : ApiController 12 | { 13 | [Route("authenticate")] 14 | public IHttpActionResult Authenticate(AuthenticateViewModel viewModel) 15 | { 16 | /* REPLACE THIS WITH REAL AUTHENTICATION 17 | ----------------------------------------------*/ 18 | if (!(viewModel.Username == "test" && viewModel.Password == "test")) 19 | { 20 | return Ok(new { success = false, message = "User code or password is incorrect" }); 21 | } 22 | 23 | return Ok(new { success = true }); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jason Watmore 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Web.Debug.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 29 | 30 | -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Web.Release.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 17 | 18 | 19 | 30 | 31 | -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | AngularJS + Web API 2 Basic HTTP Authentication Example 6 | 7 | 8 | 9 | 10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |

19 | Web API 2 Basic HTTP Authentication Example 20 |

21 |

22 | JasonWatmore.com 23 |

24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/scripts/app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // declare modules 4 | angular.module('Authentication', []); 5 | angular.module('Home', []); 6 | 7 | angular.module('BasicHttpAuthExample', [ 8 | 'Authentication', 9 | 'Home', 10 | 'ngRoute', 11 | 'ngCookies' 12 | ]) 13 | 14 | .config(['$routeProvider', function ($routeProvider) { 15 | 16 | $routeProvider 17 | .when('/login', { 18 | controller: 'LoginController', 19 | templateUrl: 'modules/authentication/views/login.html' 20 | }) 21 | 22 | .when('/', { 23 | controller: 'HomeController', 24 | templateUrl: 'modules/home/views/home.html' 25 | }) 26 | 27 | .otherwise({ redirectTo: '/login' }); 28 | }]) 29 | 30 | .run(['$rootScope', '$location', '$cookieStore', '$http', 31 | function ($rootScope, $location, $cookieStore, $http) { 32 | // keep user logged in after page refresh 33 | $rootScope.globals = $cookieStore.get('globals') || {}; 34 | if ($rootScope.globals.currentUser) { 35 | $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; // jshint ignore:line 36 | } 37 | 38 | $rootScope.$on('$locationChangeStart', function (event, next, current) { 39 | // redirect to login page if not logged in 40 | if ($location.path() !== '/login' && !$rootScope.globals.currentUser) { 41 | $location.path('/#/login'); 42 | } 43 | }); 44 | }]); -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("AngularWebAPI.WebAPI")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("AngularWebAPI.WebAPI")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("24db0e91-351a-4857-94bc-1c3f1d4ccae8")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Revision and Build Numbers 33 | // by using the '*' as shown below: 34 | [assembly: AssemblyVersion("1.0.0.0")] 35 | [assembly: AssemblyFileVersion("1.0.0.0")] 36 | -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/modules/authentication/views/login.html: -------------------------------------------------------------------------------- 1 |
2 | Username: test
3 | Password: test 4 |
5 |
{{error}}
6 |
7 |
8 | 9 | 10 | 11 | Username is required 12 |
13 |
14 | 15 | 16 | 17 | Password is required 18 |
19 |
20 | 21 | 22 |
23 |
-------------------------------------------------------------------------------- /AngularWebAPI.sln: -------------------------------------------------------------------------------- 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("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "AngularWebAPI.FrontEnd", "http://localhost:2640", "{E136325E-ABDE-4B49-8555-03F81FA89504}" 7 | ProjectSection(WebsiteProperties) = preProject 8 | UseIISExpress = "true" 9 | TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.0" 10 | Debug.AspNetCompiler.VirtualPath = "/localhost_2640" 11 | Debug.AspNetCompiler.PhysicalPath = "AngularWebAPI.FrontEnd\" 12 | Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_2640\" 13 | Debug.AspNetCompiler.Updateable = "true" 14 | Debug.AspNetCompiler.ForceOverwrite = "true" 15 | Debug.AspNetCompiler.FixedNames = "false" 16 | Debug.AspNetCompiler.Debug = "True" 17 | Release.AspNetCompiler.VirtualPath = "/localhost_2640" 18 | Release.AspNetCompiler.PhysicalPath = "AngularWebAPI.FrontEnd\" 19 | Release.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_2640\" 20 | Release.AspNetCompiler.Updateable = "true" 21 | Release.AspNetCompiler.ForceOverwrite = "true" 22 | Release.AspNetCompiler.FixedNames = "false" 23 | Release.AspNetCompiler.Debug = "False" 24 | SlnRelativePath = "AngularWebAPI.FrontEnd\" 25 | EndProjectSection 26 | EndProject 27 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AngularWebAPI.WebAPI", "AngularWebAPI.WebAPI\AngularWebAPI.WebAPI.csproj", "{5A7CA287-C8C1-4FF6-8E80-49205CB05A45}" 28 | EndProject 29 | Global 30 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 31 | Debug|Any CPU = Debug|Any CPU 32 | Release|Any CPU = Release|Any CPU 33 | EndGlobalSection 34 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 35 | {E136325E-ABDE-4B49-8555-03F81FA89504}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {E136325E-ABDE-4B49-8555-03F81FA89504}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {E136325E-ABDE-4B49-8555-03F81FA89504}.Release|Any CPU.ActiveCfg = Debug|Any CPU 38 | {E136325E-ABDE-4B49-8555-03F81FA89504}.Release|Any CPU.Build.0 = Debug|Any CPU 39 | {5A7CA287-C8C1-4FF6-8E80-49205CB05A45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {5A7CA287-C8C1-4FF6-8E80-49205CB05A45}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {5A7CA287-C8C1-4FF6-8E80-49205CB05A45}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {5A7CA287-C8C1-4FF6-8E80-49205CB05A45}.Release|Any CPU.Build.0 = Release|Any CPU 43 | EndGlobalSection 44 | GlobalSection(SolutionProperties) = preSolution 45 | HideSolutionNode = FALSE 46 | EndGlobalSection 47 | EndGlobal 48 | -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Modules/BasicAuthHttpModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Net.Http.Headers; 5 | using System.Security.Principal; 6 | using System.Text; 7 | using System.Threading; 8 | using System.Web; 9 | 10 | namespace AngularWebAPI.WebAPI.Modules 11 | { 12 | public class BasicAuthHttpModule : IHttpModule 13 | { 14 | private const string Realm = "AngularWebAPI"; 15 | 16 | public void Init(HttpApplication context) 17 | { 18 | // Register event handlers 19 | context.AuthenticateRequest += OnApplicationAuthenticateRequest; 20 | context.EndRequest += OnApplicationEndRequest; 21 | } 22 | 23 | private static void SetPrincipal(IPrincipal principal) 24 | { 25 | Thread.CurrentPrincipal = principal; 26 | if (HttpContext.Current != null) 27 | { 28 | HttpContext.Current.User = principal; 29 | } 30 | } 31 | 32 | private static bool AuthenticateUser(string credentials) 33 | { 34 | var encoding = Encoding.GetEncoding("iso-8859-1"); 35 | credentials = encoding.GetString(Convert.FromBase64String(credentials)); 36 | 37 | var credentialsArray = credentials.Split(':'); 38 | var username = credentialsArray[0]; 39 | var password = credentialsArray[1]; 40 | 41 | /* REPLACE THIS WITH REAL AUTHENTICATION 42 | ----------------------------------------------*/ 43 | if (!(username == "test" && password == "test")) 44 | { 45 | return false; 46 | } 47 | 48 | var identity = new GenericIdentity(username); 49 | SetPrincipal(new GenericPrincipal(identity, null)); 50 | 51 | return true; 52 | } 53 | 54 | private static void OnApplicationAuthenticateRequest(object sender, EventArgs e) 55 | { 56 | var request = HttpContext.Current.Request; 57 | var authHeader = request.Headers["Authorization"]; 58 | if (authHeader != null) 59 | { 60 | var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader); 61 | 62 | // RFC 2617 sec 1.2, "scheme" name is case-insensitive 63 | if (authHeaderVal.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase) && authHeaderVal.Parameter != null) 64 | { 65 | AuthenticateUser(authHeaderVal.Parameter); 66 | } 67 | } 68 | } 69 | 70 | // If the request was unauthorized, add the WWW-Authenticate header 71 | // to the response. 72 | private static void OnApplicationEndRequest(object sender, EventArgs e) 73 | { 74 | var response = HttpContext.Current.Response; 75 | if (response.StatusCode == 401) 76 | { 77 | response.Headers.Add("WWW-Authenticate", string.Format("Basic realm=\"{0}\"", Realm)); 78 | } 79 | } 80 | 81 | public void Dispose() 82 | { 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/Web.config: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | [Rr]eleases/ 14 | x64/ 15 | x86/ 16 | build/ 17 | bld/ 18 | [Bb]in/ 19 | [Oo]bj/ 20 | 21 | # Roslyn cache directories 22 | *.ide/ 23 | 24 | # MSTest test Results 25 | [Tt]est[Rr]esult*/ 26 | [Bb]uild[Ll]og.* 27 | 28 | #NUNIT 29 | *.VisualState.xml 30 | TestResult.xml 31 | 32 | # Build Results of an ATL Project 33 | [Dd]ebugPS/ 34 | [Rr]eleasePS/ 35 | dlldata.c 36 | 37 | *_i.c 38 | *_p.c 39 | *_i.h 40 | *.ilk 41 | *.meta 42 | *.obj 43 | *.pch 44 | *.pdb 45 | *.pgc 46 | *.pgd 47 | *.rsp 48 | *.sbr 49 | *.tlb 50 | *.tli 51 | *.tlh 52 | *.tmp 53 | *.tmp_proj 54 | *.log 55 | *.vspscc 56 | *.vssscc 57 | .builds 58 | *.pidb 59 | *.svclog 60 | *.scc 61 | 62 | # Chutzpah Test files 63 | _Chutzpah* 64 | 65 | # Visual C++ cache files 66 | ipch/ 67 | *.aps 68 | *.ncb 69 | *.opensdf 70 | *.sdf 71 | *.cachefile 72 | 73 | # Visual Studio profiler 74 | *.psess 75 | *.vsp 76 | *.vspx 77 | 78 | # TFS 2012 Local Workspace 79 | $tf/ 80 | 81 | # Guidance Automation Toolkit 82 | *.gpState 83 | 84 | # ReSharper is a .NET coding add-in 85 | _ReSharper*/ 86 | *.[Rr]e[Ss]harper 87 | *.DotSettings.user 88 | 89 | # JustCode is a .NET coding addin-in 90 | .JustCode 91 | 92 | # TeamCity is a build add-in 93 | _TeamCity* 94 | 95 | # DotCover is a Code Coverage Tool 96 | *.dotCover 97 | 98 | # NCrunch 99 | _NCrunch_* 100 | .*crunch*.local.xml 101 | 102 | # MightyMoose 103 | *.mm.* 104 | AutoTest.Net/ 105 | 106 | # Web workbench (sass) 107 | .sass-cache/ 108 | 109 | # Installshield output folder 110 | [Ee]xpress/ 111 | 112 | # DocProject is a documentation generator add-in 113 | DocProject/buildhelp/ 114 | DocProject/Help/*.HxT 115 | DocProject/Help/*.HxC 116 | DocProject/Help/*.hhc 117 | DocProject/Help/*.hhk 118 | DocProject/Help/*.hhp 119 | DocProject/Help/Html2 120 | DocProject/Help/html 121 | 122 | # Click-Once directory 123 | publish/ 124 | 125 | # Publish Web Output 126 | *.[Pp]ublish.xml 127 | *.azurePubxml 128 | # TODO: Comment the next line if you want to checkin your web deploy settings 129 | # but database connection strings (with potential passwords) will be unencrypted 130 | *.pubxml 131 | *.publishproj 132 | 133 | # NuGet Packages 134 | *.nupkg 135 | # The packages folder can be ignored because of Package Restore 136 | **/packages/* 137 | # except build/, which is used as an MSBuild target. 138 | !**/packages/build/ 139 | # If using the old MSBuild-Integrated Package Restore, uncomment this: 140 | #!**/packages/repositories.config 141 | 142 | # Windows Azure Build Output 143 | csx/ 144 | *.build.csdef 145 | 146 | # Windows Store app package directory 147 | AppPackages/ 148 | 149 | # Others 150 | sql/ 151 | *.Cache 152 | ClientBin/ 153 | [Ss]tyle[Cc]op.* 154 | ~$* 155 | *~ 156 | *.dbmdl 157 | *.dbproj.schemaview 158 | *.pfx 159 | *.publishsettings 160 | node_modules/ 161 | 162 | # RIA/Silverlight projects 163 | Generated_Code/ 164 | 165 | # Backup & report files from converting an old project file 166 | # to a newer Visual Studio version. Backup files are not needed, 167 | # because we have git ;-) 168 | _UpgradeReport_Files/ 169 | Backup*/ 170 | UpgradeLog*.XML 171 | UpgradeLog*.htm 172 | 173 | # SQL Server files 174 | *.mdf 175 | *.ldf 176 | 177 | # Business Intelligence projects 178 | *.rdl.data 179 | *.bim.layout 180 | *.bim_*.settings 181 | 182 | # Microsoft Fakes 183 | FakesAssemblies/ 184 | -------------------------------------------------------------------------------- /AngularWebAPI.FrontEnd/modules/authentication/services.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | angular.module('Authentication') 4 | 5 | .factory('AuthenticationService', 6 | ['Base64', '$http', '$cookieStore', '$rootScope', 7 | function (Base64, $http, $cookieStore, $rootScope) { 8 | var service = {}; 9 | 10 | service.Login = function (username, password, callback) { 11 | $http.post('/api/authenticate', { username: username, password: password }) 12 | .success(function (response) { 13 | callback(response); 14 | }); 15 | }; 16 | 17 | service.SetCredentials = function (username, password) { 18 | var authdata = Base64.encode(username + ':' + password); 19 | 20 | $rootScope.globals = { 21 | currentUser: { 22 | username: username, 23 | authdata: authdata 24 | } 25 | }; 26 | 27 | $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; // jshint ignore:line 28 | $cookieStore.put('globals', $rootScope.globals); 29 | }; 30 | 31 | service.ClearCredentials = function () { 32 | $rootScope.globals = {}; 33 | $cookieStore.remove('globals'); 34 | $http.defaults.headers.common.Authorization = 'Basic '; 35 | }; 36 | 37 | return service; 38 | }]) 39 | 40 | .factory('Base64', function () { 41 | /* jshint ignore:start */ 42 | 43 | var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 44 | 45 | return { 46 | encode: function (input) { 47 | var output = ""; 48 | var chr1, chr2, chr3 = ""; 49 | var enc1, enc2, enc3, enc4 = ""; 50 | var i = 0; 51 | 52 | do { 53 | chr1 = input.charCodeAt(i++); 54 | chr2 = input.charCodeAt(i++); 55 | chr3 = input.charCodeAt(i++); 56 | 57 | enc1 = chr1 >> 2; 58 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 59 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 60 | enc4 = chr3 & 63; 61 | 62 | if (isNaN(chr2)) { 63 | enc3 = enc4 = 64; 64 | } else if (isNaN(chr3)) { 65 | enc4 = 64; 66 | } 67 | 68 | output = output + 69 | keyStr.charAt(enc1) + 70 | keyStr.charAt(enc2) + 71 | keyStr.charAt(enc3) + 72 | keyStr.charAt(enc4); 73 | chr1 = chr2 = chr3 = ""; 74 | enc1 = enc2 = enc3 = enc4 = ""; 75 | } while (i < input.length); 76 | 77 | return output; 78 | }, 79 | 80 | decode: function (input) { 81 | var output = ""; 82 | var chr1, chr2, chr3 = ""; 83 | var enc1, enc2, enc3, enc4 = ""; 84 | var i = 0; 85 | 86 | // remove all characters that are not A-Z, a-z, 0-9, +, /, or = 87 | var base64test = /[^A-Za-z0-9\+\/\=]/g; 88 | if (base64test.exec(input)) { 89 | window.alert("There were invalid base64 characters in the input text.\n" + 90 | "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + 91 | "Expect errors in decoding."); 92 | } 93 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 94 | 95 | do { 96 | enc1 = keyStr.indexOf(input.charAt(i++)); 97 | enc2 = keyStr.indexOf(input.charAt(i++)); 98 | enc3 = keyStr.indexOf(input.charAt(i++)); 99 | enc4 = keyStr.indexOf(input.charAt(i++)); 100 | 101 | chr1 = (enc1 << 2) | (enc2 >> 4); 102 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 103 | chr3 = ((enc3 & 3) << 6) | enc4; 104 | 105 | output = output + String.fromCharCode(chr1); 106 | 107 | if (enc3 != 64) { 108 | output = output + String.fromCharCode(chr2); 109 | } 110 | if (enc4 != 64) { 111 | output = output + String.fromCharCode(chr3); 112 | } 113 | 114 | chr1 = chr2 = chr3 = ""; 115 | enc1 = enc2 = enc3 = enc4 = ""; 116 | 117 | } while (i < input.length); 118 | 119 | return output; 120 | } 121 | }; 122 | 123 | /* jshint ignore:end */ 124 | }); -------------------------------------------------------------------------------- /AngularWebAPI.WebAPI/AngularWebAPI.WebAPI.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 8 | 9 | 2.0 10 | {5A7CA287-C8C1-4FF6-8E80-49205CB05A45} 11 | {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} 12 | Library 13 | Properties 14 | AngularWebAPI.WebAPI 15 | AngularWebAPI.WebAPI 16 | v4.5 17 | true 18 | 19 | 20 | 21 | 22 | 23 | 24 | true 25 | full 26 | false 27 | bin\ 28 | DEBUG;TRACE 29 | prompt 30 | 4 31 | 32 | 33 | pdbonly 34 | true 35 | bin\ 36 | TRACE 37 | prompt 38 | 4 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll 63 | 64 | 65 | ..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll 66 | 67 | 68 | ..\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\net45\System.Web.Http.dll 69 | 70 | 71 | ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.2\lib\net45\System.Web.Http.WebHost.dll 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | Global.asax 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | Web.config 93 | 94 | 95 | Web.config 96 | 97 | 98 | 99 | 100 | 101 | 102 | 10.0 103 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | True 113 | True 114 | 2661 115 | / 116 | http://localhost:2661/ 117 | False 118 | False 119 | 120 | 121 | False 122 | 123 | 124 | 125 | 126 | 133 | --------------------------------------------------------------------------------