MiniProfiler and SqlMembershipProvider - asp.net-membership

I am trying to get any DB queries that happen when using the SqlMembershipProvider to show in MiniProfiler but I can't think of any way to swap out the SqlConnection is uses with a ProfiledDbConnection as it seems to do everything internally. Anyone have any ingenious ideas?

You could download the source (freely avaliable) for the ASP.Net SQL Providers here, read more about it here, Scott Gu published tons of whitepapers in this article about the provider model.
internal to the assembly is a SQLConnectionHelper that you can update to use the ProfiledDBConnection instead. This would give profiling to all the providers using that class, all thats left is to change your .config file to use the new provider instead.

Related

How do you build OData IEdmModel from Entity Framework model

The title says it all really. I've found several blogs with different ways (serializing the EF model to XML and then de-serializing again to the IEdmModel was one) but they're all based on old version of the OData package.
Serializing is the only way.
Example.
Relevant work.
I've ranted about this a few months ago. AFAIK nothing changed since then, and I personally don't expect them to change too much. The short story is that as of September 2012, there is no plan to use EdmLib in EF, nor is there to use EF's code in other projects.
How much should we align with OData’s EdmLib?
Not worth adopting code
Cost of implementing SSDL & MSL
Freedom to evolve our API independently
Look at aligning names of types and properties where appropriate
If your DbContext is being built from a database-first approach the given answer will fail giving this error:
Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
After some time messing with this I have found an appropriate solution. Basically you grab the CSDL resource from the assembly containing the DbContext in question and parse it using the Microsoft.Data.Edm.Csdl.CsdlReader.TryParse method. The resulting IEdmModel is valid containing the exact information given by EntityFramework when the model was built.
See here for an example with usage

Returning child elements in ASP.NET WebAPI OData

I'm using the latest ASP.Net WebAPI Nightly builds (dated 2013-01-16).
I have a simple EF database first model at the moment that has two entities - Patients and Visits. Each patient can have many visits.
I'd like to be able to query for my list of patients and have the visits entities for each patient returned inline. I know that WebAPI's OData implementation doesn't yet support $expand. I'm hoping that just means that optional client-controlled expansion is not supported and that I can force expansion server-side.
At the moment I'm not getting any of the visits inline.
For example, my PatientController's() Get() method looks like
[Queryable(AllowedQueryOptions=AllowedQueryOptions.Supported)]
public override IQueryable<Patient> Get()
{
var query = this.entities.Patients.Include("Visits");
return query;
}
I've verified that the query executing against my database does indeed include the visit information.
To use a publicly available OData service as an example, if you use the service at http://services.odata.org/OData/OData.svc/, you can get a list of Suppliers. This is http://http://services.odata.org/OData/OData.svc/Suppliers.
You can also ask for a list of suppliers that includes the list of products using http://http://services.odata.org/OData/OData.svc/Suppliers?$expand=Products
Stepping through the ASP.NET code (via the symbols server) I've got to the System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer and can see that it's CreatePropertyBag method, which builds up the list of properties to be serialized, just doesn't include the navigation properties, and they don't seem to be enumerated anywhere else apart from being written out as NavigationLinks.
I'm quite new to the ASP.NET world in general and have spent a week or so getting my head around the way things work (particularly with the changes made to OData at the end of 2012 and further changes made so far in 2013).
I suspect that if the ODataEntityTypeSerializer was to be modified (I'm happy to try) to embed this extra information in the appropriate spot (within each navigation link as an nested inline feed as best I can tell) then I'd be set.
Questions:
Have I overlooked something obvious and there's a flag I can set to turn on this behaviour? I can see why, if such a flag exists, it would be off by default (EF lazy loading and this flag wouldn't get on well)
If #1 is no, is there some other ODataEntityTypeSerializer that I could use? If so, how do I switch to it?
If #2 is no, any pointers for where I should start writing my own? Is there a place I can substitute in my own serializer or do I have to maintain my own fork of ASP.NET's Extensions project (as opposed to the Runtime project)
Thanks very much!
$expand is very high on our list of things to support for OData. But as far as I know, we don't have any flag to turn it on server-side. The formatter doesn't currently allow you to substitute your own serializers either. So I'm afraid your only option in the meantime is to create a fork and add support for $expand. If you manage to get it working, please consider sending a pull request our way:
http://aspnetwebstack.codeplex.com/SourceControl/network
You can try it already in webapi nightly builds.
Here is how to install it with nuget:
http://aspnetwebstack.codeplex.com/wikipage?title=Use%20Nightly%20Builds

How to impliment MVC Authorization when older system is in place and returns a string

I am not using any of asp.net Authentication in my code. It is handled by an outside library. I get back a success or failure from the function. So all the work is done for me.sCould I get some examples of how I would implement this in MVC3. I know a little, I have had 2 weeks experience.
Thanks.
AuthFunction("UserName", "password");
You need to implement your own MembershipProvider. The ValidateUser method will use AuthFunction method from your library.
This tutorial should be good. Just skip the repository things because those are already implemented by your library. Carefully check the configuration section at the bottom of the tutorial.

.NET 4.5, EF 5 and MembershipProvider

Does anyone know if there is going to be created a default MembershipProvider to use with EF 5 (like SqlMembershipProvider and ActiveDirectoryMembershipProvider) or we will still have to create custom ones (that is for Code First of course)?
Actually after the long comments and explanations it results that there will be a default MembershipProvider for EF and - guess what, guess what - it is called EFMembershipProvider. Here is a link.Now this is really cool because third party implementations of MembershipProvider will no longer be needed (or the respective manual implementation - it was kind of BIG and total overkill for small projects).
UPDATE
Currently it seems that this provider is not available. I do not know if it will be developed and included in the future either.
Since we are encouraged to use SimpleMembershipProvider and migrations when using EF Code First that is what I am doing now. You can also implement the ExtendedMembershipProvider, which requires a little bit more effort.
For me the best solution for now is to inherit SimpleMembershipProvider and modify only the things that I need (I am using most of the code from my previous implementation of MembershipProvider), for example logging with email or username.
I've implemented a CodeFirst MembershipProvider & Role with my Silversite CMS ASP.NET library, that can be found at silversite.codeplex.com. The library also supports multiple DbContexts for CodeFirst databases.
As far as I know though currently the implementation is broken, and I didn't have the time to fix it yet. Also Profile and Session providers still are lacking. But I've got the code from the MySql providers that should not be too hard to port.

Sample Code for Creating Custom Membership Provider

I am writing an MVC 3 application and I am trying to implement my own custom membership provider (following the sample in Apress' Pro ASP.NET MVC 3 Framework).
I created my custom class, inherited from MembershipProvider and (using ReSharper) implemented all 27 of the methods with NotImplementedExceptions.
Now, I have overridden the ValidateUser method as the book states and it works fine, but I would like to make my provider more robust and implement the other methods (e.g. set MinRequiredPasswordLength and GetNumberOfUsersOnline).
Is there some sample code that I can use to start populating these methods that I can tweak to fit my own DB/Model schema?
I can certainly use trial and error to figure it out, but a base code sample would greatly help.
EDIT:
This question was just downvoted twice. If you are going to downvote, please post a comment as to why so that I can work on improving my questions.
EDIT 2:
For example, for the following method:
public override int GetNumberOfUsersOnline()
{
throw new System.NotImplementedException();
}
I can try to write code from scratch to look at some web log, determine the users login time and approximate if they are still on, but that will take a large amount of time for me to figure out. Since all this code is from the same interface that Microsoft wrote for the standard SqlMembershipProvider, isn't there code out there (even from MS) that contains this method? If so, I want take it, modify it so it uses my DB schema instead of the aspnetdb default schema. I would prefer to have some sort of base code to work from. I thought this would be a simple and fairly standard request, but perhaps not.
You can't use the default provider's code for your custom provider, that is why you are implementing a custom one, to tweak it according to your requirements, use your own db tables etc.
Take a look at my blog posts about custom membership, custom role providers and custom membership user. There is an example there of how you can use your own database to get/set membership information.
Here's a of sample implementation of custom MembershipProvider on MSDN.
http://msdn.microsoft.com/en-us/library/6tc47t75.aspx

Resources