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

V2 #21

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

V2 #21

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
125 changes: 125 additions & 0 deletions IdentityDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Configuration;
using System.Linq;
using MongoDB.Driver;

namespace MongoDB.AspNet.Identity
{
public class IdentityDbContext<TUser> : IdentityDbContext<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
where TUser : IdentityUser
{
public IdentityDbContext() : this("DefaultConnection") { }
public IdentityDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
}

public class IdentityDbContext : IdentityDbContext<IdentityUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public IdentityDbContext() : this("DefaultConnection") { }
public IdentityDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { }
}

public class IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> : IDisposable
where TUser : IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>
where TRole : IdentityRole<TKey, TUserRole>
where TUserLogin : IdentityUserLogin<TKey>
where TUserRole : IdentityUserRole<TKey>
where TUserClaim : IdentityUserClaim<TKey>
{

internal readonly MongoDatabase db;

public MongoDatabase Context { get { return db; } }


/// <summary>
/// Gets the database from connection string.
/// </summary>
/// <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)
{
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);
}

/// <summary>
/// Gets the database from URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns>MongoDatabase.</returns>
private MongoDatabase 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
}

/// <summary>
/// Uses connectionString to connect to server and then uses databae name specified.
/// </summary>
/// <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)
{
var client = new MongoClient(connectionString);
MongoServer server = client.GetServer();
return server.GetDatabase(dbName);
}

public bool RequireUniqueEmail
{
get;
set;
}

public virtual IQueryable<TRole> Roles
{
get;
set;
}

public virtual IQueryable<TUser> Users
{
get;
set;
}

public IdentityDbContext() : this("DefaultConnection") { }
public IdentityDbContext(string nameOrConnectionString)
{
if (nameOrConnectionString.ToLower().StartsWith("mongodb://"))
{
db = GetDatabaseFromUrl(new MongoUrl(nameOrConnectionString));
}
else
{
string connStringFromManager =
ConfigurationManager.ConnectionStrings[nameOrConnectionString].ConnectionString;
if (connStringFromManager.ToLower().StartsWith("mongodb://"))
{
db = GetDatabaseFromUrl(new MongoUrl(connStringFromManager));
}
else
{
db = GetDatabaseFromSqlStyle(connStringFromManager);
}
}
}

public void Dispose()
{
}
}
}
42 changes: 42 additions & 0 deletions IdentityRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.AspNet.Identity;

namespace MongoDB.AspNet.Identity
{
public class IdentityRole : IdentityRole<string, IdentityUserRole>
{
public IdentityRole()
{
base.Id = Guid.NewGuid().ToString();
}

public IdentityRole(string roleName)
: this()
{
base.Name = roleName;
}
}


public class IdentityRole<TKey, TUserRole> : IRole<TKey>
where TUserRole : IdentityUserRole<TKey>
{
public TKey Id { get; set; }
public string Name { get; set; }

public ICollection<TUserRole> Users { get; set; }

public virtual ICollection<TUserRole> GetUsers()
{
return Users;
}

public IdentityRole()
{
Users = new List<TUserRole>();
}

}
}
140 changes: 94 additions & 46 deletions IdentityUser.cs
Original file line number Diff line number Diff line change
@@ -1,102 +1,150 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Identity;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;


namespace MongoDB.AspNet.Identity
{

/// <summary>
/// Class IdentityUser.
/// </summary>
public class IdentityUser : IUser
public class IdentityUser : IdentityUser<string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUser, IUser<string>
{
/// <summary>
/// Unique key for the user
/// Initializes a new instance of the <see cref="IdentityUser"/> class.
/// </summary>
public IdentityUser() {}

/// <summary>
/// Initializes a new instance of the <see cref="IdentityUser"/> class.
/// </summary>
/// <param name="userName">Name of the user.</param>
public IdentityUser(string userName) : this()
{
this.UserName = userName;
}
}

/// <summary>
/// Class IdentityUser.
/// </summary>
public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
where TLogin : IdentityUserLogin<TKey>
where TRole : IdentityUserRole<TKey>
where TClaim : IdentityUserClaim<TKey>
{
/// <summary>
/// Unique key for the user. TKey must be a string.
/// </summary>
/// <value>The identifier.</value>
/// <returns>The unique key for the user</returns>
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public virtual string Id { get; set; }
public virtual TKey Id { get; set; }

/// <summary>
/// Gets or sets the name of the user.
/// </summary>
/// <value>The name of the user.</value>
public virtual string UserName { get; set; }
public virtual string UserName { get; set; }

/// <summary>
/// Gets or sets the password hash.
/// </summary>
/// <value>The password hash.</value>
public virtual string PasswordHash { get; set; }
public virtual string PasswordHash { get; set; }

/// <summary>
/// Gets or sets the security stamp.
/// </summary>
/// <value>The security stamp.</value>
public virtual string SecurityStamp { get; set; }
public virtual string SecurityStamp { get; set; }

/// <summary>
/// Gets the roles.
/// Gets the roles. Extended from the AspNet IdentityUser entity to add a Role array to the users to follow a more Mongo document model style.
/// </summary>
/// <value>The roles.</value>
public virtual List<string> Roles { get; private set; }
public virtual List<string> Roles { get; private set; }

/// <summary>
/// Gets or sets the roles. Matches the AspNet IdentityUser entity signature.
/// </summary>
/// <value>The roles.</value>
//public virtual ICollection<TRole> Roles { get; set; }

/// <summary>
/// Gets the claims.
/// </summary>
/// <value>The claims.</value>
public virtual List<IdentityUserClaim> Claims { get; private set; }
public virtual List<IdentityUserClaim> Claims { get; private set; }

/// <summary>
/// Gets the logins.
/// </summary>
/// <value>The logins.</value>
public virtual List<UserLoginInfo> Logins { get; private set; }
public virtual List<UserLoginInfo> Logins { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="IdentityUser"/> class.
/// Gets or sets a value indicating whether [two factor enabled].
/// </summary>
public IdentityUser()
{
this.Claims = new List<IdentityUserClaim>();
this.Roles = new List<string>();
this.Logins = new List<UserLoginInfo>();
}
/// <value><c>true</c> if [two factor enabled]; otherwise, <c>false</c>.</value>
public virtual bool TwoFactorEnabled { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="IdentityUser"/> class.
/// Gets or sets the phone number.
/// </summary>
/// <param name="userName">Name of the user.</param>
public IdentityUser(string userName) : this()
{
this.UserName = userName;
}
}
/// <value>The phone number.</value>
public virtual string PhoneNumber { get; set; }

/// <summary>
/// Class IdentityUserClaim.
/// </summary>
public class IdentityUserClaim
{
/// <summary>
/// Gets or sets the identifier.
/// Gets or sets a value indicating whether [phone number confirmed].
/// </summary>
/// <value>The identifier.</value>
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public virtual string Id { get; set; }
/// <value><c>true</c> if [phone number confirmed]; otherwise, <c>false</c>.</value>
public virtual bool PhoneNumberConfirmed { get; set; }

/// <summary>
/// Gets or sets the user identifier.
/// Gets or sets the email.
/// </summary>
/// <value>The user identifier.</value>
public virtual string UserId { get; set; }
/// <value>The email.</value>
public virtual string Email { get; set; }

/// <summary>
/// Gets or sets the type of the claim.
/// Gets or sets a value indicating whether [email confirmed].
/// </summary>
/// <value>The type of the claim.</value>
public virtual string ClaimType { get; set; }
/// <value><c>true</c> if [email confirmed]; otherwise, <c>false</c>.</value>
public virtual bool EmailConfirmed { get; set; }

/// <summary>
/// Gets or sets the claim value.
/// Gets or sets the access failed count.
/// </summary>
/// <value>The claim value.</value>
public virtual string ClaimValue { get; set; }

}
/// <value>The access failed count.</value>
public virtual int AccessFailedCount { get; set; }

/// <summary>
/// Gets or sets a value indicating whether [lockout enabled].
/// </summary>
/// <value><c>true</c> if [lockout enabled]; otherwise, <c>false</c>.</value>
public virtual bool LockoutEnabled { get; set; }

/// <summary>
/// Gets or sets the lockout end date UTC.
/// </summary>
/// <value>The lockout end date UTC.</value>
public virtual DateTime? LockoutEndDateUtc { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="IdentityUser"/> class.
/// </summary>
public IdentityUser()
{
//this.Id = Guid.NewGuid().ToString();
this.Claims = new List<IdentityUserClaim>();
this.Roles = new List<string>();
this.Logins = new List<UserLoginInfo>();
}
}

}
Loading