No WebSecurity.GetAllUsers() method. Why? - asp.net-webpages

I want to list all users who are in admin role using WebMatrix.WebData.Security.
I am able see Membership.GetAllUsers() Method is there. But not WebSecurity.GetAllUsers().Why?.
I used Membership.GetAllUsers () method but this throws an exception “Specified method is not supported.”
Can anybody provide alternate ways to achieve this?

Here you can use like
var userRoles = (SimpleRoleProvider)Roles.Provider;
var userName = userRoles.GetUsersInRole("admin");
Here userName would be string array.

Below code may helpful to someone. I used lambda expression.
List<UserProfile> UserProfileList = db.UserProfileRepository.Where(u => Roles.IsUserInRole(u.UserName, "admin") == true).ToList();

Related

Casting with Single<> on RxJava

I would like to know if there is a way to do a cast from Single<Object> to Single<CustomClass>.
I have a class that implements a method that should return a Single<Customer>, I implemented the search like here
Single.create(single -> {
CustomerServiceDto customer = mapper.map(customerRepository.findById(id).get(), CustomerServiceDto.class);
single.onSuccess(customer);
});
There isn't any problem. It's what I need. This create returns me a Single<Customer> but when I implement another function to handling an exception
Single.create(single -> {
CustomerServiceDto customer = mapper.map(customerRepository.findById(id).get(), CustomerServiceDto.class);
single.onSuccess(customer);
}).onErrorReturn(error -> new CustomerServiceDto());
It returns me a Single<Object>. Can I do a casting here? To avoid change the method's signature. I tried with the classic (Single<Customer>) Single<Object> instance, but it isn't work. Thanks for your advice.
The answer was the #dano's comment. Thanks, #dano.

Return in a map

Is it possible to directly return a response inside a map otherwise than doing that:
var authorized = false
roles.map { role =>
val method = userRole.getClass.getDeclaredMethod(role.toString)
authorized = method.invoke(userRole).asInstanceOf[Boolean]
}
authorized
or is it the only way? I've learned that it's better to avoid using var.
Thanks!
If you want to check if there exists an element in your list that satisfies some condition, you can use the exists method:
list.exists(value => condition(value))
Edit after the question was changed:
You can still use exists for this case, but if you want to invoke all the methods, you need to use map first (assuming your list is eager):
roles.map { role =>
userRole.getClass.getDeclaredMethod(role.toString).invoke(userRole)
}.exists(_.asInstanceOf[Boolean])
If you don't need to call all methods (which you probably don't need to if the methods are pure), you can just use exists:
roles.exists { role =>
userRole.getClass.getDeclaredMethod(role.toString)
.invoke(userRole).asInstanceOf[Boolean]
}

Detecting direct instantiation with nDepend

With the nDepend API, would something like the following be possible?
I want to keep a watch out for instances where our object factory has been bypassed and a concrete class is being instantiated directly.
Obviously I'd need to be able to filter out things like:
StringBuilder stringBuilder = new StringBuilder();
perhaps by adding to the Where clause type names to exclude, or namespaces in which to check, but I want to make sure we see:
IMyCustomType item = ObjectFactory.Get<IMyCustomType>();
and not this:
MyCustomType item = new MyCustomType();
Thanks.
Maybe such code rule below could help you, hopefully it is understandable enough to not have to comment it:
warnif count > 0
let ctors = Application.Namespaces.WithNameLike("Namespaces1*").ChildMethods().Where(m => m.IsConstructor)
let codeThatMustNotCallCtors = Application.Namespaces.WithNameLike("Namespaces2*").ChildMethods()
from m in codeThatMustNotCallCtors.UsingAny(ctors)
select new { m, ctorsCalled = m.MethodsCalled.Intersect(ctors ) }

MongoDB C# - update using custom strongly-typed objects not allowed?

I am trying to perform an update using strongly-typed objects. For example,
public void setAppointmentPrefs(string UserName, IEnumerable<AppointmentInfo> info)
{
var query = new QueryDocument {{ "ProviderId", UserName}};
var update = Update.Set("Prefs",prefs); // prefs.toList() gives same error
// providerprefs initialized in constructor
providerprefs.Update(query, update);
}
I receive a compiler error saying:Error 14 The best overloaded method match for 'MongoDB.Driver.Builders.Update.Set(string, MongoDB.Bson.BsonValue)' has some invalid arguments
Obviously the Mongo driver will not let me update based on my own object (whether as IEnumerable or prefs.toList()), which seems a contrast from the way it permits me to insert or query with custom objects. Surely I am missing something obvious that would permit me to avoid deserializing, weakly typing then creating a generic BsonDocument!! TIA.
You can do an Update based on your own types! Have you tried using the typed Query and Update builders?
Try something like this:
var query = Query<AppointmentInfo>.EQ(i => i.ProviderId, userName);
var update = Update<AppointmentInfo>.Set(i => i.Prefs, info.Prefs);
Not sure I got the types and everything write from your partial code, but that should give you the general idea.
Let me know if you have any further questions.
I know this has been answered but I for one don't fully understand Roberts answer.
All I did is call the "ToBsonDocument()" method for it to except the object as a parameter
So:
customObject.ToBsonDocument()
If you have an array of objects inside a document:
var query = Query.EQ("_id", ObjectId.Parse(id.ToString()));
var update = Update.Push("ArrayOfObjects", customObject.ToBsonDocument());
collection.Update(query, update);

FluentMongo throwing error all of a sudden

I am using FluentMongo and the MongoDBCSharpDriver. My code was working fine for a while, but after updating my MongoCSharpDriver, I now I keep getting this error when I try to query the database:
"Discriminators can only be registered for classes, not for interface MyLib.Services.IRepoData."
The interface IRepoData is just one that I use for all my objects saved to MongoDB. It just defines _id for everything. Here is the line that is breaking:
var item = Collection.AsQueryable().SingleOrDefault(a => a.Id == itemID);
Can anyone shed some light on this one? If I just use .SingleOrDefault() with no lambda then it works fine, its passing a lambda that breaks it.
EDIT
In case this helps...
var Collection = GetCollection<MyClass>();
private MongoCollection<T> GetCollection<T>() where T : class, new()
{
string typeName = typeof(T).Name;
var collection = db.GetCollection<T>(typeName, safeMode);
return collection;
}
Found it! I was calling GetCollection() from within another generic method, like this:
public T Save<T>(T item) where T : class, IRepoData, new()
{
GetCollection<T>().Save(item);
}
This caused GetCollection to see T as the interface instead of the actual instance class. GetCollection works fine anywhere else.
For anyone else with this problem, I just used a low level query like this instead... Collection.FindOneAs<T>(Query.EQ("Id", itemID.ToString()));