WebSecurity SimpleMembershipProvider not working together? - asp.net

Getting a bit disappointed with all the weird exception I keep running into while working with WebSecurity. Also the poor integration with OAuth doesn't make it look prettier. Considering to drop the concept and write the whole user management manually...
Anyway, I am using WebSecurity to administer users and passwords. Now I try to implement the part where accounts can be deleted. Weirdly enough this method is not on the static class WebSecurity. Apparently I need to delete accounts via the SimpleMembershipProvider.
var provider = new SimpleMembershipProvider();
provider.DeleteAccount(username);
The deleteAccount method throws an invalidoperation exception with the following message:
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call
any other method of the "WebSecurity" class. This call should be placed in an
_AppStart.cshtml file in the root of your site.
Well that's weird since I already have this in my _ViewStart (otherwise I wouldn't have been able to create the accounts in the first place).
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection(
"DefaultConnection",
"Users",
"UserId",
"UserName",
true);
}
What am I doing wrong this time?

Have you tried:
Membership.DeleteUser(username);

Related

Cannot open database requested by the login .Login failed for user

I have few aspx pages and and few tables in database. Each page connects to respective table. The purpose of each page is the same, only some condition are different. When I develop I use different database for testing data and called it "Database_DEV1" to be easy to remember. Now a problem is that only one page connects to the database (Database_DEV1) with same connection string. Other pages throws error with message:
Cannot open database "Database" requested by the login. The login failed.
Login failed for user 'xxx'
Why does it connect to "Database" if I provided connection string that refers to Database_DEV1?
Is it possible that this kind of error may appear at all? Do I miss something? I checked connection string I provided.
It looks like page "remembers" something different for some reason. I tried simple SqlConnection class and it works, but dbml file not.
update
Problem is that that Context refers to folder:
// C:\Users\xxx\AppData\Local\Temp\Temporary ASP.NET Files\root\0f053840_shadow\2edebf24\5576\App_Code.whnwybua.dll
At first, application connects using context that is declared in dbml. But when I do some actions it tries to connect using context which refers to dll file I described above. Why is that?

Create Database If Not Exist Without Restarting Application

I am creating dynamic connection strings in my project. They're created on the fly with the information provided specifically for every user. When the application first fires off, if a database doesn't exist (first time user logs on), a new database is created without problems with this initializer:
public DataContext() : base()
{
// ProxyCreation and LazyLoading doesn't affect the situation so
// comments may be removed
//this.Configuration.LazyLoadingEnabled = false;
//this.Configuration.ProxyCreationEnabled = false;
string conStr = GetDb();
this.Database.Connection.ConnectionString = conStr;
}
The problem is, with this method, I have to restart the application pool on the server and the new user should be the first accessor to the application.
I need the same thing without a requirement of restarting the app. Is that possible?
(This is a SPA using AngularJS on MVC views and WebApi as data provider - May be relevant somehow, so thought I should mention)
I already tried this, but this creates an error for EF and the application doesn't start at all...
You could try a little bit different approach to connect directly (and create) the right database.
public class DataContext : DbContext
{
public DataContext(DbConnection connection) : base(connection, true) { }
}
Here you create the DbContext already with the right connection.
Take also care because you need to specify to migrations that the right connection should be used (not the Web.Config connection but the connection that raised the database creation).
See the second overload here https://msdn.microsoft.com/en-US/library/hh829099(v=vs.113).aspx#M:System.Data.Entity.MigrateDatabaseToLatestVersion.

Dynamics SqlDataDictionaryPermission failure

I am trying to truncate one of our tables in a class to run as a batch job, but keep getting a "Request for permission of type SqlDataDictionaryPermission failed. This is on an AX 4.0 system. I followed MSDN example on acquiring permissions and I am an admin. Here's the code:
//Truncate table
new SqlDataDictionaryPermission(
methodstr(SqlDataDictionary, tableTruncate)).assert();
sqlDict = new SqlDataDictionary();
sqlDict.tableTruncate(tableNum(PMF_INVENTTABLEMODULELOG),false);
CodeAccessPermission::revertAssert();
As I stated, I have admin access to this, so should have the required security key. Though this is displaying the (usr) environment in the class list.
Are you sure it is running server side?
You did not expose your method definition.
I stumbled upon this issue in Ax 2009, while trying to set up a batch that reindexes tables from parameters.
The method doesn't need to be static or have server predicate. The only thing that works is setting the class "RunOn" parameter as "Server".
I know it's and old question with low views, but perhaps some poor soul can be saved.

What does it mean when a Collection insert results in a 404?

I have a very simple test app in meteor, and I've created a collection like so:
var people = new Meteor.Collection("people");
When I try to do a simple insert, like this:
people.insert({name: "Benson"});
I get a 404 error with the text "Method not found". I admit there's a good chance I've fat-fingered something here, but I'd love to know both what's wrong, and why the error is so opaque (i.e. where it's coming from).
This error almost certainly means you've only defined people on the client, but not on the server. The new Meteor.Collection('people') declaration has to also run on the server, or else the server doesn't know how to run your insert command.
Be sure you're calling new Meteor.Collection on both the client and the server. Are you calling it inside if (Meteor.is_client), or in a file under the client subdirectory?
Some more details: On the server, new Meteor.Collection defines three remote methods (Meteor.methods) that insert, update, and remove documents in the named MongoDB collection. On the client, the same command creates an in-memory minimongo collection that lives inside the browser, and defines three stubs that simulate the methods by applying the same change to the minimongo collection. By only declaring the collection on the client, your client code runs the local insert just fine, but when it asks the server to perform the real insert, the server has no idea what method you've asked it to execute.
If you want to use Collection only on Client side and you don't need to save that data to server you can declare your collection in "client" folder or in .isClient() function by passing null to the constructor like this:
if(Meteor.isClient()){
// Some other code
...
onlyClientCollection = new Meteor.Collection(null);
// Some other code
...
}

Problem with Unit testing of ASP.NET project (NullReferenceException when running the test)

I'm trying to create a bunch of MS visual studio unit tests for my n-tiered web app but for some reason I can't run those tests and I get the following error -
"Object reference not set to an
instance of an object"
What I'm trying to do is testing of my data access layer where I use LINQ data context class to execute a certain function and return a result,however during the debugging process I found out that all the tests fail as soon as they get to the LINQ data context class and it has something to do with the connection string but I cant figure out what is the problem.
The debugging of tests fails here(the second line):
public EICDataClassesDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["EICDatabaseConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
And my test is as follows:
TestMethod()]
public void OnGetCustomerIDTest()
{
FrontLineStaffDataAccess target = new FrontLineStaffDataAccess(); // TODO: Initialize to an appropriate value
string regNo = "jonh"; // TODO: Initialize to an appropriate value
int expected = 10; // TODO: Initialize to an appropriate value
int actual;
actual = target.OnGetCustomerID(regNo);
Assert.AreEqual(expected, actual);
}
The method which I call from DAL is:
public int OnGetCustomerID(string regNo)
{
using (LINQDataAccess.EICDataClassesDataContext dataContext = new LINQDataAccess.EICDataClassesDataContext())
{
IEnumerable<LINQDataAccess.GetCustomerIDResult> sProcCustomerIDResult = dataContext.GetCustomerID(regNo);
int customerID = sProcCustomerIDResult.First().CustomerID;
return customerID;
}
}
So basically everything fails after it reaches the 1st line of DA layer method and when it tries to instantiate the LINQ data access class...
I've spent around 10 hours trying to troubleshoot the problem but no result...I would really appreciate any help!
UPDATE:
Finally I've fixed this!!!!:) I dont know why but for some reasons in the app.config file the connection to my database was as follows:
AttachDbFilename=|DataDirectory|\EICDatabase.MDF
So what I did is I just changed the path and instead of |DataDirectory| I put the actual path where my MDF file sits,i.e
C:\Users\1\Documents\Visual Studio 2008\Projects\EICWebSystem\EICWebSystem\App_Data\EICDatabase.mdf
After I had done that it worked out!But still it's a bit not clear what was the problem...probably incorrect path to the database?My web.config of ASP.NET project contains the |DataDirectory|\EICDatabase.MDF path though..
Is LINQDataAccess.EICDataClassesDataContext looking to the web.config or some other outside source of data for its setup?
I can tell you for a fact that you must jump thru hoops to get web.config accessible to your test code.
Update
Ah, yes. I see that you're using ConfigurationManager on the line where your test fails... ConfigurationManager looks to web.config for configuration. This has been a sticking point for me when I write my tests.
You need to either change the code so that the class can be instantiated without web.config, or you need to make it so that your tests can access web.config.
Does your test project have it's own configuration file? This type of behavior usually means the app can't find the connection string. Test projects require their own file since they are not running in the context of the client app.
UPDATE The error you describe after adding an app.config is common when testing web applications built on SQLExpress and attaching an .mdf. SQLExpress cannot be run in more than one process at a time. So if you have previously run your web application it may still be active and will conflict with the attempt to attach the database.
You can use SQL Management Studio to attach the database permanently and then use a more traditional connection string like:
Server=myServer;Database=EICDatabase;Trusted_Connection=True;
For me it seems like your problem is the connection string, which is not set.
I assume your unit test is in a different project than the DAL.
You call the 'new' command on the datacontext constructor without a connection string. So it should usually use its default, when set. But since this setting normally is stored in the web.config of the other project there is no connection string set and you get the error.
If its right, what i assume, you have to get the settings from the DAL project into the unit-testing project. Simplest solution should be to copy web.config connection string to app.config of unit test project.
There are other possibilities for sure, but i think its not easy to get the web.config configuration into your unit-testing project.

Resources