Domanda

Following this post, SimpleMemberShipProvider, Using multiple PropertyValues with custom Database tables, I successfully create a new user using the SimpleMembershipProvider and WebMatrix.WebData.WebSecurity.

WebSecurity.CreateUserAndAccount(model.UserName, model.Password,new { FirstName = model.FirstName,LastName = model.LastName,AppId = Guid.NewGuid(), MemberId = model.MemberId});

However, I have not been able to find any built-in methods for retrieving this information back from MembershipProvider or WebSecurity methods. I know I could query the database directly to get the information back and update, but then my code would become tightly coupled with this test implementation.

Anyone know how to get extended properties from a currently logged in user in the SimpleMembershipProvider? Thanks.

È stato utile?

Soluzione

SBirthare is right, however, since he doesn't explain how to do this, allow me (this is assuming you use the C# server-side language option, which you tagged, so I believe you do. This also assumes that the table name in which the "FirstName" and "LastName" columns belong is, in fact, "UserProfile"):

Since WebMatrix comes with SQL Server-CE, we can just use a simple SQL query for this.

First set up your connection to the database:

var db = Database.Open("Users");

Next, compile the appropriate string to be used for the query (a simple string variable is used):

string selectQueryString = "SELECT FirstName, LastName FROM UserProfile";

The above string when passed to the Query() C# method (shown below), will however return a list (specifically IEnumerable<dynamic>) of all "FirstName"s and "LastName"s returned from the database. If you only wanted certain rows returned you can use the WHERE clause in the SQL string, so instead of the above string you could make this instead (this is just an example, you will have to tailor your own conditions to fit your situation, but this should give you an easy example):

string selectQueryString = "SELECT FirstName, LastName FROM UserProfile WHERE Email = @0 AND UserID = @1";

First let me address what the @0 and the @1 is, in case you don't already know. They are placeholders that will end up being filled with the conditions you want to test against (more on this further down) using this method, known as "paramaterized queries", this will be your smoking gun against SQL injection attacks. In short: ALWAYS USE THIS METHOD FOR CONDITIONS TESTED IN THE WHERE CLAUSE (I really can't stress this enough).

Now, I'm not sure how much you know about SQL querying, but they can be very powerful and useful and by using things like LIKE, ORDER BY, GROUP BY, JOIN, UNION, AND, OR, or even subqueries, as well as, many many other keywords and approaches, you can really make the SQL query return just about anything you desire, in the order you desire (a great beginner SQL query tutorial can be found here: http://www.sqlcourse.com/index.html).

Okay, moving on... Once you've written the selectQueryString the way that you want to, all you need left is to store it, like so:

int ID = 6;
string email = "testEmailName@gmail.com";
var queryResultsList = db.Query(selectQueryString, email, ID); //Here the first argument passed to the `db.Query()` method is the query string, but each additional argument passed (and you can actually pass an array of values here instead if you want) is used to fill the `@0` and `@1` placeholders you formed in your string earlier (order is ever important here!).

And display it, like so:

foreach (var row in queryResultsList)
{
    //Do something for each row returned, here!
    <div>FIRST NAME: @row.FirstName</div><br/>
    <div>LAST NAME: @row.LastName</div>
{

Sorry if this is a lot to take in, but I thought I would at least show you all of the steps that you need to actually make this happen. Really there isn't that much to it, I just felt like giving you a bunch of helpful pointers a long the way :)

Anyway, just let me know if you have any questions or if I need to clarify anything.

EDIT:

Well, I just saw that you use MVC as opposed to Web-Pages. My answer is from the Web-Pages environment, but to be honest, I'm not sure if it would be any different or not because I have never worked with MVC. Either way, the SQL query itself should be the same and since we're both using C#, idk.

Either way, if I am told that this answer doesn't fit the scenario or is not useful to you, I will be glad to delete it.

Altri suggerimenti

As far as I know you will have to query the table using your current ORM framework. SimpleMembershipProvider and WebSecurity allows you basic stuff e.g. WebSecurity.CurrentUserName, WebSecurity.UserExists, WebSecurity.GetUserId, etc.

The custom column you add into any of the tables created by SimpleMembershipProvider have to be fetched manually from the table.

SimpleMembershipProvider have no idea that you added FirstName and LastName into UserProfile (or other table) table.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top