Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Updated integer primary key implementation to be multi-tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
JSkimming committed Nov 26, 2013
1 parent 05a6002 commit 9d85330
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ namespace IntegerPkImplementation.Controllers
public class AccountController : Controller
{
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
: this(new UserManager<ApplicationUser, int>(new ApplicatonUserStore(new ApplicationDbContext()) { TenantId = 1 }))
{
}

public AccountController(UserManager<ApplicationUser> userManager)
public AccountController(UserManager<ApplicationUser, int> userManager)
{
UserManager = userManager;
}

public UserManager<ApplicationUser> UserManager { get; private set; }
public UserManager<ApplicationUser, int> UserManager { get; private set; }

//
// GET: /Account/Login
Expand Down Expand Up @@ -102,7 +102,7 @@ public async Task<ActionResult> Register(RegisterViewModel model)
public async Task<ActionResult> Disassociate(string loginProvider, string providerKey)
{
ManageMessageId? message = null;
IdentityResult result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey));
IdentityResult result = await UserManager.RemoveLoginAsync(User.Identity.GetUserIdInt(), new UserLoginInfo(loginProvider, providerKey));
if (result.Succeeded)
{
message = ManageMessageId.RemoveLoginSuccess;
Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task<ActionResult> Manage(ManageUserViewModel model)
{
if (ModelState.IsValid)
{
IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserIdInt(), model.OldPassword, model.NewPassword);
if (result.Succeeded)
{
return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
Expand All @@ -164,7 +164,7 @@ public async Task<ActionResult> Manage(ManageUserViewModel model)

if (ModelState.IsValid)
{
IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserIdInt(), model.NewPassword);
if (result.Succeeded)
{
return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
Expand Down Expand Up @@ -237,7 +237,7 @@ public async Task<ActionResult> LinkLoginCallback()
{
return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
}
var result = await UserManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login);
var result = await UserManager.AddLoginAsync(User.Identity.GetUserIdInt(), loginInfo.Login);
if (result.Succeeded)
{
return RedirectToAction("Manage");
Expand Down Expand Up @@ -304,7 +304,7 @@ public ActionResult ExternalLoginFailure()
[ChildActionOnly]
public ActionResult RemoveAccountList()
{
var linkedAccounts = UserManager.GetLogins(User.Identity.GetUserId());
var linkedAccounts = UserManager.GetLogins(User.Identity.GetUserIdInt());
ViewBag.ShowRemoveButton = HasPassword() || linkedAccounts.Count > 1;
return (ActionResult)PartialView("_RemoveAccountPartial", linkedAccounts);
}
Expand Down Expand Up @@ -348,7 +348,7 @@ private void AddErrors(IdentityResult result)

private bool HasPassword()
{
var user = UserManager.FindById(User.Identity.GetUserId());
var user = UserManager.FindById(User.Identity.GetUserIdInt());
if (user != null)
{
return user.PasswordHash != null;
Expand Down
27 changes: 27 additions & 0 deletions src/Examples/IntegerPkImplementation/IdentityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using Microsoft.AspNet.Identity;

namespace IntegerPkImplementation
{
public static class IdentityExtensions
{
public static int GetUserIdInt(this IIdentity identity)
{
if (identity == null)
throw new ArgumentNullException("identity");

string stringUserId = identity.GetUserId();

int userId;
if (string.IsNullOrWhiteSpace(stringUserId) || !int.TryParse(stringUserId, out userId))
{
return default(int);
}

return userId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
</Reference>
<Reference Include="Microsoft.AspNet.Identity.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.AspNet.Identity.Core.1.0.0\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
<HintPath>..\..\packages\Microsoft.AspNet.Identity.Core.1.1.0-alpha1-131122\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.Identity.EntityFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.AspNet.Identity.EntityFramework.1.0.0\lib\net45\Microsoft.AspNet.Identity.EntityFramework.dll</HintPath>
<HintPath>..\..\packages\Microsoft.AspNet.Identity.EntityFramework.1.1.0-alpha1-131122\lib\net45\Microsoft.AspNet.Identity.EntityFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNet.Identity.Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -178,6 +178,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="IdentityExtensions.cs" />
<Compile Include="Models\AccountViewModels.cs" />
<Compile Include="Models\IdentityModels.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -242,6 +243,12 @@
<Content Include="packages.config" />
<None Include="Project_Readme.html" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AspNet.Identity.EntityFramework.Multitenant\AspNet.Identity.EntityFramework.Multitenant.csproj">
<Project>{ec024226-3fe4-4283-bd11-44987d082295}</Project>
<Name>AspNet.Identity.EntityFramework.Multitenant</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
32 changes: 30 additions & 2 deletions src/Examples/IntegerPkImplementation/Models/IdentityModels.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using AspNet.Identity.EntityFramework.Multitenant;

namespace IntegerPkImplementation.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
public class ApplicationUser : MultitenantIdentityUser<int, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
public class ApplicationUserRole : IdentityUserRole<int>
{
}

public class ApplicationUserLogin : MultitenantIdentityUserLogin<int, int>
{
}

public class ApplicationUserClaim : IdentityUserClaim<int>
{
}

public class ApplicationRole : IdentityRole<int, ApplicationUserRole>
{
}

public class ApplicatonUserStore :
MultitenantUserStore<ApplicationUser, ApplicationRole, int, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicatonUserStore(DbContext context)
: base(context)
{
}
}

public class ApplicationDbContext
: MultitenantIdentityDbContext<ApplicationUser, ApplicationRole, int, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
Expand Down
4 changes: 2 additions & 2 deletions src/Examples/IntegerPkImplementation/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<package id="EntityFramework" version="6.0.1" targetFramework="net45" />
<package id="jQuery" version="2.0.3" targetFramework="net45" />
<package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.Core" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.Core" version="1.1.0-alpha1-131122" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="1.1.0-alpha1-131122" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.Owin" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
Expand Down

0 comments on commit 9d85330

Please sign in to comment.