Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating to latest versions #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
16 changes: 10 additions & 6 deletions MongoDB.AspNet.Identity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@
<DocumentationFile>bin\MongoDB.AspNet.Identity.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNet.Identity.Core">
<HintPath>packages\Microsoft.AspNet.Identity.Core.1.0.0\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
<Reference Include="Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>packages\Microsoft.AspNet.Identity.Core.2.2.0\lib\net45\Microsoft.AspNet.Identity.Core.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Bson, Version=1.8.3.9, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
<Reference Include="MongoDB.Bson, Version=2.0.0.828, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>packages\mongocsharpdriver.1.8.3\lib\net35\MongoDB.Bson.dll</HintPath>
<HintPath>packages\MongoDB.Bson.2.0.0\lib\net45\MongoDB.Bson.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Driver, Version=1.8.3.9, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
<Reference Include="MongoDB.Driver, Version=2.0.0.828, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>packages\mongocsharpdriver.1.8.3\lib\net35\MongoDB.Driver.dll</HintPath>
<HintPath>packages\MongoDB.Driver.2.0.0\lib\net45\MongoDB.Driver.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Driver.Core">
<HintPath>packages\MongoDB.Driver.Core.2.0.0\lib\net45\MongoDB.Driver.Core.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System" />
Expand Down
63 changes: 27 additions & 36 deletions UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
using Microsoft.AspNet.Identity;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace MongoDB.AspNet.Identity
{
/// <summary>
/// Class UserStore.
/// </summary>
/// <typeparam name="TUser">The type of the t user.</typeparam>
public class UserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>,
public class UserStore<TUser> : IUserStore<TUser>, IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>,
IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>
where TUser : IdentityUser
{
Expand All @@ -24,7 +23,7 @@ public class UserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>,
/// <summary>
/// The database
/// </summary>
private readonly MongoDatabase db;
private readonly IMongoDatabase db;

/// <summary>
/// The _disposed
Expand All @@ -42,32 +41,29 @@ public class UserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>,
/// <param name="connectionString">The connection string.</param>
/// <returns>MongoDatabase.</returns>
/// <exception cref="System.Exception">No database name specified in connection string</exception>
private MongoDatabase GetDatabaseFromSqlStyle(string connectionString)
private IMongoDatabase GetDatabaseFromSqlStyle(string connectionString)
{
var conString = new MongoConnectionStringBuilder(connectionString);
MongoClientSettings settings = MongoClientSettings.FromConnectionStringBuilder(conString);
MongoServer server = new MongoClient(settings).GetServer();
if (conString.DatabaseName == null)
MongoUrl url = MongoUrl.Create(connectionString);
if (url.DatabaseName == null)
{
throw new Exception("No database name specified in connection string");
}
return server.GetDatabase(conString.DatabaseName);
MongoClientSettings settings = MongoClientSettings.FromUrl(url);
return new MongoClient(settings).GetDatabase(url.DatabaseName);
}

/// <summary>
/// Gets the database from URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns>MongoDatabase.</returns>
private MongoDatabase GetDatabaseFromUrl(MongoUrl url)
private IMongoDatabase GetDatabaseFromUrl(MongoUrl url)
{
var client = new MongoClient(url);
MongoServer server = client.GetServer();
if (url.DatabaseName == null)
{
throw new Exception("No database name specified in connection string");
}
return server.GetDatabase(url.DatabaseName); // WriteConcern defaulted to Acknowledged
return new MongoClient(url).GetDatabase(url.DatabaseName); // WriteConcern defaulted to Acknowledged
}

/// <summary>
Expand All @@ -76,22 +72,22 @@ private MongoDatabase GetDatabaseFromUrl(MongoUrl url)
/// <param name="connectionString">The connection string.</param>
/// <param name="dbName">Name of the database.</param>
/// <returns>MongoDatabase.</returns>
private MongoDatabase GetDatabase(string connectionString, string dbName)
private IMongoDatabase GetDatabase(string connectionString, string dbName)
{
var client = new MongoClient(connectionString);
MongoServer server = client.GetServer();
return server.GetDatabase(dbName);
return client.GetDatabase(dbName);
}

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="UserStore{TUser}" /> class. Uses DefaultConnection name if none was
/// specified.
/// </summary>
public UserStore() : this("DefaultConnection")
public UserStore()
: this("DefaultConnection")
{
}

Expand Down Expand Up @@ -144,13 +140,13 @@ public UserStore(string connectionNameOrUrl, string dbName)
/// Initializes a new instance of the <see cref="UserStore{TUser}"/> class using a already initialized Mongo Database.
/// </summary>
/// <param name="mongoDatabase">The mongo database.</param>
public UserStore(MongoDatabase mongoDatabase)
public UserStore(IMongoDatabase mongoDatabase)
{
db = mongoDatabase;
}


/// <summary>
/// <summary>
/// Initializes a new instance of the <see cref="UserStore{TUser}" /> class.
/// </summary>
/// <param name="connectionName">Name of the connection from ConfigurationManager.ConnectionStrings[].</param>
Expand Down Expand Up @@ -246,7 +242,7 @@ public Task CreateAsync(TUser user)
if (user == null)
throw new ArgumentNullException("user");

db.GetCollection<TUser>(collectionName).Insert(user);
db.GetCollection<TUser>(collectionName).InsertOneAsync(user);

return Task.FromResult(user);
}
Expand All @@ -263,7 +259,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<TUser>(collectionName).DeleteOneAsync(x => x.Id == user.Id);
return Task.FromResult(true);
}

Expand All @@ -275,8 +271,8 @@ public Task DeleteAsync(TUser user)
public Task<TUser> FindByIdAsync(string userId)
{
ThrowIfDisposed();
TUser user = db.GetCollection<TUser>(collectionName).FindOne((Query.EQ("_id", ObjectId.Parse(userId))));
return Task.FromResult(user);

return db.GetCollection<TUser>(collectionName).Find(u => u.Id == userId).FirstOrDefaultAsync();
}

/// <summary>
Expand All @@ -287,9 +283,9 @@ public Task<TUser> FindByIdAsync(string userId)
public Task<TUser> FindByNameAsync(string userName)
{
ThrowIfDisposed();
TUser user = db.GetCollection<TUser>(collectionName).FindOne((Query.EQ("UserName", userName)));
return Task.FromResult(user);

return
db.GetCollection<TUser>(collectionName).Find(u => u.UserName == userName).FirstOrDefaultAsync();
}

/// <summary>
Expand All @@ -305,7 +301,7 @@ public Task UpdateAsync(TUser user)
throw new ArgumentNullException("user");

db.GetCollection<TUser>(collectionName)
.Update(Query.EQ("_id", ObjectId.Parse(user.Id)), Update.Replace(user), UpdateFlags.Upsert);
.ReplaceOneAsync(x => x.Id == user.Id, user);

return Task.FromResult(user);
}
Expand Down Expand Up @@ -346,13 +342,9 @@ public Task AddLoginAsync(TUser user, UserLoginInfo login)
/// <returns>Task{`0}.</returns>
public Task<TUser> FindAsync(UserLoginInfo login)
{
TUser user = null;
user =
db.GetCollection<TUser>(collectionName)
.FindOne(Query.And(Query.EQ("Logins.LoginProvider", login.LoginProvider),
Query.EQ("Logins.ProviderKey", login.ProviderKey)));

return Task.FromResult(user);
return
db.GetCollection<TUser>(collectionName)
.Find<TUser>(u => u.Logins.Any(l => l.LoginProvider == login.LoginProvider && l.ProviderKey == login.ProviderKey) == true).FirstOrDefaultAsync();
}

/// <summary>
Expand Down Expand Up @@ -546,4 +538,3 @@ private void ThrowIfDisposed()
#endregion
}
}

6 changes: 4 additions & 2 deletions packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Identity.Core" version="1.0.0" targetFramework="net45" />
<package id="mongocsharpdriver" version="1.8.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.0" targetFramework="net45" />
<package id="MongoDB.Bson" version="2.0.0" targetFramework="net45" />
<package id="MongoDB.Driver" version="2.0.0" targetFramework="net45" />
<package id="MongoDB.Driver.Core" version="2.0.0" targetFramework="net45" />
</packages>