Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/KriaSoft.AspNet.Identity.DbFirst/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Configuration(IAppBuilder app)
app.CreatePerOwinContext<ApplicationDbContext>(() => new ApplicationDbContext());
app.CreatePerOwinContext<UserManager<User, int>>(
(IdentityFactoryOptions<UserManager<User, int>> options, IOwinContext context) =>
new UserManager<User, int>(new UserStore(context.Get<ApplicationDbContext>())));
new UserManager<User, int>(new UserStore(context.Get<ApplicationDbContext>(), "Example")));

// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(new OAuthAuthorizationServerOptions
Expand Down
2 changes: 1 addition & 1 deletion examples/KriaSoft.AspNet.Identity.DbFirst/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
<configuration>
<connectionStrings>
<add name="ApplicationDbContext" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDb)\v11.0;initial catalog=AspNetIdentity;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="ApplicationDbContext" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\mssql2014;initial catalog=AspNetIdentity;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseName>AspNetIdentity</TargetDatabaseName>
<DeployScriptFileName>Database.sql</DeployScriptFileName>
<TargetConnectionString>Data Source=(LocalDb)\v11.0;Integrated Security=True;Pooling=False;Connect Timeout=30</TargetConnectionString>
<TargetConnectionString>Data Source=localhost\mssql2014;Integrated Security=True;Pooling=False;Connect Timeout=30</TargetConnectionString>
<BlockWhenDriftDetected>True</BlockWhenDriftDetected>
<RegisterDataTierApplication>True</RegisterDataTierApplication>
<ProfileVersionNumber>1</ProfileVersionNumber>
Expand Down
16 changes: 10 additions & 6 deletions src/KriaSoft.AspNet.Identity.Database/Reference Data/User.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ MERGE INTO [dbo].[User] AS Target
USING (VALUES

(1, N'admin', N'admin@example.com', 0, N'ACe+kHUdH61ms8NbkXSCXyV34CEP7tjfj93JrtlKRPfShGurFdAujQrmbVA7J9MDbg==',
N'9771f91d-b4a0-45e0-8971-899b907c5863', NULL, 0, 0, NULL, 0, 0),
N'9771f91d-b4a0-45e0-8971-899b907c5863', NULL, 0, 0, NULL, 0, 0, 'Example'),
(2, N'user', N'user@example.com', 0, N'ACe+kHUdH61ms8NbkXSCXyV34CEP7tjfj93JrtlKRPfShGurFdAujQrmbVA7J9MDbg==',
N'9771f91d-b4a0-45e0-8971-899b907c5863', NULL, 0, 0, NULL, 0, 0)
N'9771f91d-b4a0-45e0-8971-899b907c5863', NULL, 0, 0, NULL, 0, 0, 'Example')

) AS Source (
[UserID],
Expand All @@ -21,7 +21,8 @@ USING (VALUES
[TwoFactorEnabled],
[LockoutEndDateUtc],
[LockoutEnabled],
[AccessFailedCount]
[AccessFailedCount],
[TenantId]
)
ON Target.[UserID] = Source.[UserID]
-- Update matched rows
Expand All @@ -37,7 +38,8 @@ UPDATE SET
[TwoFactorEnabled] = Source.[TwoFactorEnabled],
[LockoutEndDateUtc] = Source.[LockoutEndDateUtc],
[LockoutEnabled] = Source.[LockoutEnabled],
[AccessFailedCount] = Source.[AccessFailedCount]
[AccessFailedCount] = Source.[AccessFailedCount],
[TenantId] = Source.[TenantId]
-- Insert new rows
WHEN NOT MATCHED BY TARGET THEN
INSERT (
Expand All @@ -52,7 +54,8 @@ INSERT (
[TwoFactorEnabled],
[LockoutEndDateUtc],
[LockoutEnabled],
[AccessFailedCount]
[AccessFailedCount],
[TenantId]
)
VALUES (
[UserID],
Expand All @@ -66,7 +69,8 @@ VALUES (
[TwoFactorEnabled],
[LockoutEndDateUtc],
[LockoutEnabled],
[AccessFailedCount]
[AccessFailedCount],
[TenantId]
);
GO

Expand Down
1 change: 1 addition & 0 deletions src/KriaSoft.AspNet.Identity.Database/Tables/User.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[LockoutEnabled] [dbo].[Flag] NOT NULL,
[AccessFailedCount] INT NOT NULL,

[TenantId] NVARCHAR(50) NOT NULL,
CONSTRAINT [PK_User_UserID] PRIMARY KEY CLUSTERED ([UserID] ASC),
CONSTRAINT [UK_User_UserName] UNIQUE NONCLUSTERED ([UserName] ASC)
);
2 changes: 1 addition & 1 deletion src/KriaSoft.AspNet.Identity.EntityFramework/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ApplicationDbContext" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDb)\v11.0;initial catalog=AspNetIdentity;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="ApplicationDbContext" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\mssql2014;initial catalog=AspNetIdentity;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/KriaSoft.AspNet.Identity.EntityFramework/Model.edmx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Application.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<Schema Namespace="Application.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityType Name="User">
<Key>
<PropertyRef Name="UserID" />
Expand All @@ -21,6 +21,7 @@
<Property Name="LockoutEndDateUtc" Type="datetime" />
<Property Name="LockoutEnabled" Type="bit" Nullable="false" />
<Property Name="AccessFailedCount" Type="int" Nullable="false" />
<Property Name="TenantId" Type="nvarchar" MaxLength="50" Nullable="false" />
</EntityType>
<EntityType Name="UserClaim">
<Key>
Expand Down Expand Up @@ -158,6 +159,7 @@
<NavigationProperty Name="Claims" Relationship="Self.FK_UserClaim_User" FromRole="User" ToRole="UserClaim" />
<NavigationProperty Name="Logins" Relationship="Self.FK_UserLogin_User" FromRole="User" ToRole="UserLogin" />
<NavigationProperty Name="Roles" Relationship="Self.UserUserRole" FromRole="User" ToRole="UserRole" />
<Property Name="TenantId" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" />
</EntityType>
<EntityType Name="UserClaim">
<Key>
Expand Down Expand Up @@ -247,6 +249,7 @@
<EntitySetMapping Name="Users">
<EntityTypeMapping TypeName="Application.User">
<MappingFragment StoreEntitySet="User">
<ScalarProperty Name="TenantId" ColumnName="TenantId" />
<ScalarProperty Name="Id" ColumnName="UserID" />
<ScalarProperty Name="UserName" ColumnName="UserName" />
<ScalarProperty Name="Email" ColumnName="Email" />
Expand Down
1 change: 1 addition & 0 deletions src/KriaSoft.AspNet.Identity.EntityFramework/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public User()
public Nullable<System.DateTime> LockoutEndDateUtc { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public string TenantId { get; set; }

public virtual ICollection<UserClaim> Claims { get; set; }
public virtual ICollection<UserLogin> Logins { get; set; }
Expand Down
34 changes: 26 additions & 8 deletions src/KriaSoft.AspNet.Identity.EntityFramework/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ public partial class UserStore :
{
private readonly ApplicationDbContext db;

public UserStore(ApplicationDbContext db)
public virtual string TenantId { get; set; }
/// <summary>
/// Throws exceptions if the state of the object is invalid or has been disposed.
/// </summary>
private void ThrowIfInvalid()
{
if (db == null)
{
throw new ArgumentNullException("db");
}
if (EqualityComparer<string>.Default.Equals(TenantId, default(string)))
throw new InvalidOperationException("The TenantId has not been set.");
}

public UserStore(ApplicationDbContext db, string TenantId)
{
if (db == null) { throw new ArgumentNullException("db"); }
this.db = db;

if (TenantId == null) { throw new ArgumentNullException("TenantId"); }
this.TenantId = TenantId;

}



//// IQueryableUserStore<User, int>

public IQueryable<User> Users
Expand Down Expand Up @@ -60,9 +72,11 @@ public Task<User> FindByIdAsync(int userId)

public Task<User> FindByNameAsync(string userName)
{
ThrowIfInvalid();

return this.db.Users
.Include(u => u.Logins).Include(u => u.Roles).Include(u => u.Claims)
.FirstOrDefaultAsync(u => u.UserName == userName);
.FirstOrDefaultAsync(u => u.UserName == userName && u.TenantId == TenantId);
}

public Task UpdateAsync(User user)
Expand Down Expand Up @@ -128,6 +142,8 @@ public async Task<User> FindAsync(UserLoginInfo login)
throw new ArgumentNullException("login");
}

ThrowIfInvalid();

var provider = login.LoginProvider;
var key = login.ProviderKey;

Expand All @@ -140,7 +156,7 @@ public async Task<User> FindAsync(UserLoginInfo login)

return await this.db.Users
.Include(u => u.Logins).Include(u => u.Roles).Include(u => u.Claims)
.FirstOrDefaultAsync(u => u.Id.Equals(userLogin.UserId));
.FirstOrDefaultAsync(u => u.Id.Equals(userLogin.UserId) && u.TenantId == TenantId);
}

public Task<IList<UserLoginInfo>> GetLoginsAsync(User user)
Expand Down Expand Up @@ -336,9 +352,11 @@ public Task SetSecurityStampAsync(User user, string stamp)

public Task<User> FindByEmailAsync(string email)
{
ThrowIfInvalid();

return this.db.Users
.Include(u => u.Logins).Include(u => u.Roles).Include(u => u.Claims)
.FirstOrDefaultAsync(u => u.Email == email);
.FirstOrDefaultAsync(u => u.Email == email && u.TenantId == TenantId);
}

public Task<string> GetEmailAsync(User user)
Expand Down