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 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..e362d17 100644 --- a/UserStore.cs +++ b/UserStore.cs @@ -16,15 +16,15 @@ 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, IUserStore where TUser : IdentityUser { #region Private Methods & Variables /// /// The database /// - private readonly MongoDatabase db; + private readonly MongoDatabase _db; /// /// The _disposed @@ -34,27 +34,9 @@ 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) - { - 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); - } - - /// + /// /// Gets the database from URL. /// /// The URL. @@ -104,20 +86,12 @@ public UserStore(string connectionNameOrUrl) { if (connectionNameOrUrl.ToLower().StartsWith("mongodb://")) { - db = GetDatabaseFromUrl(new MongoUrl(connectionNameOrUrl)); + _db = GetDatabaseFromUrl(new MongoUrl(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)); } } @@ -132,11 +106,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 +120,7 @@ public UserStore(string connectionNameOrUrl, string dbName) /// The mongo database. public UserStore(MongoDatabase mongoDatabase) { - db = mongoDatabase; + _db = mongoDatabase; } @@ -159,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 @@ -246,7 +213,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 +230,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 +242,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 +255,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 +271,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 +311,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 +497,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