From 12c695c014c5851621ac4dbff287af0cb4116e81 Mon Sep 17 00:00:00 2001 From: jose mauricio delgado Date: Thu, 26 Nov 2015 19:24:54 -0600 Subject: [PATCH 1/3] Updated to latest MongoDB Driver, latest AspNet Identity library. Updated User store to implement new interfaces such as IuserEmailStore Added new properties to the IdentityUser to work with the new interfaces. --- IdentityUser.cs | 41 +++++- MongoDB.AspNet.Identity.csproj | 26 +++- UserStore.cs | 262 +++++++++++++++++++++++++++------ packages.config | 7 +- 4 files changed, 276 insertions(+), 60 deletions(-) diff --git a/IdentityUser.cs b/IdentityUser.cs index aa31b8c..0856611 100644 --- a/IdentityUser.cs +++ b/IdentityUser.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Microsoft.AspNet.Identity; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; @@ -49,10 +50,42 @@ public class IdentityUser : IUser /// /// The logins. public virtual List Logins { get; private set; } + /// + /// Gets the email + /// + public string Email { get; set; } + /// + /// Gets the phone number + /// + public string PhoneNumber { get; set; } + /// + /// gets the email confirmed boolean + /// + public bool EmailConfirmed { get; set; } + /// + /// gets the phone confirmed boolean + /// + public bool PhoneNumberConfirmed { get; set; } + /// + /// gets two factor authentication boolean + /// + public bool TwoFactorEnabled { get; set; } + /// + /// gets date time offset + /// + public DateTimeOffset LockoutEndDateUtc { get; set; } + /// + /// gets failed counts + /// + public int AccessFailedCount { get; set; } + /// + /// gets lockout enabled boolean + /// + public bool LockoutEnabled { get; set; } - /// - /// Initializes a new instance of the class. - /// + /// + /// Initializes a new instance of the class. + /// public IdentityUser() { this.Claims = new List(); diff --git a/MongoDB.AspNet.Identity.csproj b/MongoDB.AspNet.Identity.csproj index ba38609..3af57bf 100644 --- a/MongoDB.AspNet.Identity.csproj +++ b/MongoDB.AspNet.Identity.csproj @@ -20,6 +20,7 @@ + true @@ -41,16 +42,25 @@ bin\MongoDB.AspNet.Identity.XML - - packages\Microsoft.AspNet.Identity.Core.1.0.0\lib\net45\Microsoft.AspNet.Identity.Core.dll + + packages\Microsoft.AspNet.Identity.Core.2.2.1\lib\net45\Microsoft.AspNet.Identity.Core.dll + True - - False - packages\mongocsharpdriver.1.8.3\lib\net35\MongoDB.Bson.dll + + packages\MongoDB.Bson.2.1.1\lib\net45\MongoDB.Bson.dll + True - - False - packages\mongocsharpdriver.1.8.3\lib\net35\MongoDB.Driver.dll + + packages\MongoDB.Driver.2.1.1\lib\net45\MongoDB.Driver.dll + True + + + packages\MongoDB.Driver.Core.2.1.1\lib\net45\MongoDB.Driver.Core.dll + True + + + packages\mongocsharpdriver.2.1.1\lib\net45\MongoDB.Driver.Legacy.dll + True diff --git a/UserStore.cs b/UserStore.cs index 93613eb..1112381 100644 --- a/UserStore.cs +++ b/UserStore.cs @@ -16,15 +16,16 @@ namespace MongoDB.AspNet.Identity /// /// The type of the t user. public class UserStore : IUserLoginStore, IUserClaimStore, IUserRoleStore, - IUserPasswordStore, IUserSecurityStampStore - where TUser : IdentityUser + IUserPasswordStore, IUserSecurityStampStore, IUserEmailStore, IUserPhoneNumberStore, IUserTwoFactorStore, + IUserLockoutStore + where TUser : IdentityUser { #region Private Methods & Variables /// /// The database /// - private readonly MongoDatabase db; + private readonly MongoDatabase _db; /// /// The _disposed @@ -34,24 +35,18 @@ public class UserStore : IUserLoginStore, IUserClaimStore, /// /// The AspNetUsers collection name /// - private const string collectionName = "AspNetUsers"; + private const string CollectionName = "AspNetUsers"; - /// - /// Gets the database from connection string. - /// - /// The connection string. - /// MongoDatabase. - /// No database name specified in connection string - private MongoDatabase GetDatabaseFromSqlStyle(string connectionString) + /// + /// Gets the database from connection string. + /// + /// The connection string. + /// MongoDatabase. + /// No database name specified in connection string + [Obsolete("Do not use .NET style connection strings, use URL style connection strings")] + private MongoDatabase GetDatabaseFromSqlStyle(string connectionString) { - var conString = new MongoConnectionStringBuilder(connectionString); - MongoClientSettings settings = MongoClientSettings.FromConnectionStringBuilder(conString); - MongoServer server = new MongoClient(settings).GetServer(); - if (conString.DatabaseName == null) - { - throw new Exception("No database name specified in connection string"); - } - return server.GetDatabase(conString.DatabaseName); + throw new Exception("Use URI style connection strings as described on https://docs.mongodb.org/manual/reference/connection-string/"); } /// @@ -104,7 +99,7 @@ public UserStore(string connectionNameOrUrl) { if (connectionNameOrUrl.ToLower().StartsWith("mongodb://")) { - db = GetDatabaseFromUrl(new MongoUrl(connectionNameOrUrl)); + _db = GetDatabaseFromUrl(new MongoUrl(connectionNameOrUrl)); } else { @@ -112,11 +107,11 @@ public UserStore(string connectionNameOrUrl) ConfigurationManager.ConnectionStrings[connectionNameOrUrl].ConnectionString; if (connStringFromManager.ToLower().StartsWith("mongodb://")) { - db = GetDatabaseFromUrl(new MongoUrl(connStringFromManager)); + _db = GetDatabaseFromUrl(new MongoUrl(connStringFromManager)); } else { - db = GetDatabaseFromSqlStyle(connStringFromManager); + _db = GetDatabaseFromSqlStyle(connStringFromManager); } } } @@ -132,11 +127,11 @@ public UserStore(string connectionNameOrUrl, string dbName) { if (connectionNameOrUrl.ToLower().StartsWith("mongodb://")) { - db = GetDatabase(connectionNameOrUrl, dbName); + _db = GetDatabase(connectionNameOrUrl, dbName); } else { - db = GetDatabase(ConfigurationManager.ConnectionStrings[connectionNameOrUrl].ConnectionString, dbName); + _db = GetDatabase(ConfigurationManager.ConnectionStrings[connectionNameOrUrl].ConnectionString, dbName); } } @@ -146,7 +141,7 @@ public UserStore(string connectionNameOrUrl, string dbName) /// The mongo database. public UserStore(MongoDatabase mongoDatabase) { - db = mongoDatabase; + _db = mongoDatabase; } @@ -162,11 +157,11 @@ public UserStore(string connectionName, bool useMongoUrlFormat) if (useMongoUrlFormat) { var url = new MongoUrl(connectionString); - db = GetDatabaseFromUrl(url); + _db = GetDatabaseFromUrl(url); } else { - db = GetDatabaseFromSqlStyle(connectionString); + _db = GetDatabaseFromSqlStyle(connectionString); } } @@ -246,7 +241,7 @@ public Task CreateAsync(TUser user) if (user == null) throw new ArgumentNullException("user"); - db.GetCollection(collectionName).Insert(user); + _db.GetCollection(CollectionName).Insert(user); return Task.FromResult(user); } @@ -263,7 +258,7 @@ public Task DeleteAsync(TUser user) if (user == null) throw new ArgumentNullException("user"); - db.GetCollection(collectionName).Remove((Query.EQ("_id", ObjectId.Parse(user.Id)))); + _db.GetCollection(CollectionName).Remove((Query.EQ("_id", ObjectId.Parse(user.Id)))); return Task.FromResult(true); } @@ -275,7 +270,7 @@ public Task DeleteAsync(TUser user) public Task FindByIdAsync(string userId) { ThrowIfDisposed(); - TUser user = db.GetCollection(collectionName).FindOne((Query.EQ("_id", ObjectId.Parse(userId)))); + TUser user = _db.GetCollection(CollectionName).FindOne((Query.EQ("_id", ObjectId.Parse(userId)))); return Task.FromResult(user); } @@ -288,7 +283,7 @@ public Task FindByNameAsync(string userName) { ThrowIfDisposed(); - TUser user = db.GetCollection(collectionName).FindOne((Query.EQ("UserName", userName))); + TUser user = _db.GetCollection(CollectionName).FindOne((Query.EQ("UserName", userName))); return Task.FromResult(user); } @@ -304,7 +299,7 @@ public Task UpdateAsync(TUser user) if (user == null) throw new ArgumentNullException("user"); - db.GetCollection(collectionName) + _db.GetCollection(CollectionName) .Update(Query.EQ("_id", ObjectId.Parse(user.Id)), Update.Replace(user), UpdateFlags.Upsert); return Task.FromResult(user); @@ -344,18 +339,15 @@ public Task AddLoginAsync(TUser user, UserLoginInfo login) /// /// The login. /// Task{`0}. - public Task FindAsync(UserLoginInfo login) - { - TUser user = null; - user = - db.GetCollection(collectionName) - .FindOne(Query.And(Query.EQ("Logins.LoginProvider", login.LoginProvider), - Query.EQ("Logins.ProviderKey", login.ProviderKey))); + public Task FindAsync(UserLoginInfo login) { + var user = _db.GetCollection(CollectionName) + .FindOne(Query.And(Query.EQ("Logins.LoginProvider", login.LoginProvider), + Query.EQ("Logins.ProviderKey", login.ProviderKey))); - return Task.FromResult(user); + return Task.FromResult(user); } - /// + /// /// Gets the logins asynchronous. /// /// The user. @@ -533,11 +525,189 @@ public Task SetSecurityStampAsync(TUser user, string stamp) return Task.FromResult(0); } - /// - /// Throws if disposed. - /// - /// - private void ThrowIfDisposed() + /// + /// + /// + /// + /// + public virtual Task GetEmailConfirmedAsync(TUser user) { + return Task.FromResult(user.EmailConfirmed); + } + + /// + /// + /// + /// + /// + /// + public virtual Task SetEmailConfirmedAsync(TUser user, bool confirmed) { + user.EmailConfirmed = confirmed; + return Task.FromResult(0); + } + + /// + /// + /// + /// + /// + /// + public virtual Task SetEmailAsync(TUser user, string email) { + user.Email = email; + return Task.FromResult(0); + } + + /// + /// + /// + /// + /// + public virtual Task GetEmailAsync(TUser user) { + return Task.FromResult(user.Email); + } + + /// + /// + /// + /// + /// + public virtual Task FindByEmailAsync(string email) { + return Task.FromResult(_db.GetCollection("AspNeTUsers").FindOne(Query.EQ("email", (BsonValue)email))); + } + + /// + /// + /// + /// + /// + /// + public virtual Task SetPhoneNumberAsync(TUser user, string phoneNumber) { + user.PhoneNumber = phoneNumber; + return Task.FromResult(0); + } + + /// + /// + /// + /// + /// + public virtual Task GetPhoneNumberAsync(TUser user) { + return Task.FromResult(user.PhoneNumber); + } + + /// + /// + /// + /// + /// + public virtual Task GetPhoneNumberConfirmedAsync(TUser user) { + return Task.FromResult(user.PhoneNumberConfirmed); + } + + /// + /// + /// + /// + /// + /// + public virtual Task SetPhoneNumberConfirmedAsync(TUser user, bool confirmed) { + user.PhoneNumberConfirmed = confirmed; + return Task.FromResult(0); + } + + /// + /// + /// + /// + /// + /// + public virtual Task SetTwoFactorEnabledAsync(TUser user, bool enabled) { + user.TwoFactorEnabled = enabled; + return Task.FromResult(0); + } + + /// + /// + /// + /// + /// + public virtual Task GetTwoFactorEnabledAsync(TUser user) { + return Task.FromResult(user.TwoFactorEnabled); + } + + /// + /// + /// + /// + /// + public virtual Task GetLockoutEndDateAsync(TUser user) { + return Task.FromResult(user.LockoutEndDateUtc); + } + + /// + /// + /// + /// + /// + /// + public virtual Task SetLockoutEndDateAsync(TUser user, DateTimeOffset lockoutEnd) { + user.LockoutEndDateUtc = new DateTime(lockoutEnd.Ticks, DateTimeKind.Utc); + return Task.FromResult(0); + } + + /// + /// + /// + /// + /// + public virtual Task IncrementAccessFailedCountAsync(TUser user) { + user.AccessFailedCount++; + return Task.FromResult(user.AccessFailedCount); + } + + /// + /// + /// + /// + /// + public virtual Task ResetAccessFailedCountAsync(TUser user) { + user.AccessFailedCount = 0; + return Task.FromResult(0); + } + + /// + /// + /// + /// + /// + public virtual Task GetAccessFailedCountAsync(TUser user) { + return Task.FromResult(user.AccessFailedCount); + } + + /// + /// + /// + /// + /// + public virtual Task GetLockoutEnabledAsync(TUser user) { + return Task.FromResult(user.LockoutEnabled); + } + + /// + /// + /// + /// + /// + /// + public virtual Task SetLockoutEnabledAsync(TUser user, bool enabled) { + user.LockoutEnabled = enabled; + return Task.FromResult(0); + } + + /// + /// Throws if disposed. + /// + /// + private void ThrowIfDisposed() { if (_disposed) throw new ObjectDisposedException(GetType().Name); diff --git a/packages.config b/packages.config index 0e77e46..84c34a5 100644 --- a/packages.config +++ b/packages.config @@ -1,5 +1,8 @@  - - + + + + + \ No newline at end of file From 9177976d751b1f2512f50ce9ea40af2b629c5061 Mon Sep 17 00:00:00 2001 From: jose mauricio delgado Date: Thu, 26 Nov 2015 19:33:24 -0600 Subject: [PATCH 2/3] ignoring application host file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 81b9783..f504957 100644 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,4 @@ _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML MongoDB.AspNet.Identity.csproj.GhostDoc.xml +.vs/config/applicationhost.config From faab2baa14e26d47e6171bdbe249f7089a540e6e Mon Sep 17 00:00:00 2001 From: jose mauricio delgado Date: Thu, 26 Nov 2015 23:32:38 -0600 Subject: [PATCH 3/3] Removed usage of get connection sql style, added implementation for IUserStore --- UserStore.cs | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/UserStore.cs b/UserStore.cs index 1112381..e362d17 100644 --- a/UserStore.cs +++ b/UserStore.cs @@ -17,8 +17,7 @@ namespace MongoDB.AspNet.Identity /// The type of the t user. public class UserStore : IUserLoginStore, IUserClaimStore, IUserRoleStore, IUserPasswordStore, IUserSecurityStampStore, IUserEmailStore, IUserPhoneNumberStore, IUserTwoFactorStore, - IUserLockoutStore - where TUser : IdentityUser + IUserLockoutStore, IUserStore where TUser : IdentityUser { #region Private Methods & Variables @@ -37,19 +36,7 @@ public class UserStore : IUserLoginStore, IUserClaimStore, /// private const string CollectionName = "AspNetUsers"; - /// - /// Gets the database from connection string. - /// - /// The connection string. - /// MongoDatabase. - /// No database name specified in connection string - [Obsolete("Do not use .NET style connection strings, use URL style connection strings")] - private MongoDatabase GetDatabaseFromSqlStyle(string connectionString) - { - throw new Exception("Use URI style connection strings as described on https://docs.mongodb.org/manual/reference/connection-string/"); - } - - /// + /// /// Gets the database from URL. /// /// The URL. @@ -103,16 +90,8 @@ public UserStore(string connectionNameOrUrl) } else { - string connStringFromManager = - ConfigurationManager.ConnectionStrings[connectionNameOrUrl].ConnectionString; - if (connStringFromManager.ToLower().StartsWith("mongodb://")) - { - _db = GetDatabaseFromUrl(new MongoUrl(connStringFromManager)); - } - else - { - _db = GetDatabaseFromSqlStyle(connStringFromManager); - } + string connStringFromManager = ConfigurationManager.ConnectionStrings[connectionNameOrUrl].ConnectionString; + _db = GetDatabaseFromUrl(new MongoUrl(connStringFromManager)); } } @@ -154,15 +133,8 @@ public UserStore(MongoDatabase mongoDatabase) public UserStore(string connectionName, bool useMongoUrlFormat) { string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString; - if (useMongoUrlFormat) - { - var url = new MongoUrl(connectionString); - _db = GetDatabaseFromUrl(url); - } - else - { - _db = GetDatabaseFromSqlStyle(connectionString); - } + var url = new MongoUrl(connectionString); + _db = GetDatabaseFromUrl(url); } #endregion