I need to get user id from asp.net mvc 3 app in console app. Here is my code: (mvc)
public class MembershipRepository : IMembershipRepository
{
public Guid GetUserId(string name)
{
var providerUserKey = Membership.GetUser(name).ProviderUserKey; // HttpException
if (providerUserKey != null)
return (Guid)providerUserKey;
throw new NullReferenceException("user with this username does't exist");
}
}
(console) code:
ninjectKernel.Bind<IMembershipRepository>().To<MembershipRepository>();
var membershipRepository = ninjectKernel.Get<IMembershipRepository>();
Guid userId = membershipRepository.GetUserId("admin");
HttpException: Unable to connect to the Sql server database.
Before the exception occures the window pops up: This program has compatibility problems (microsoft sql seqver 2008 and 2008 r2).
I have sql server 2012 installed.
Besides, it’s only a small chunk of code. Before I’m trying to get userId I use other repositories to add data to the database and it works: when I check the table contents it’s full of new entries.
How do I need to change the code? Mabby there is some other way to get userId in console app?
Thanks!
Edits:
App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add
name="WebStoreConnectionString" connectionString="Data Source=(LocalDB)\v11.0; DataBase=WebStore;
AttachDbFilename=C:\Users\Aleksey\repos\working_copy\WebStore\WebStore\App_Data\WebStore.mdf;
Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
</configuration>
The exception occured because in MembershipRepository I used built-in Membership provider to return Users. I changed the impementation to work with data context:
everything works, and I doesn’t need to use web request)
So, you can create new instance of MembershipRepository or instance of YourAppNameDataContext (and workd directly with data context)