Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi. Here is my login controller:

SQL
public ActionResult Login(Login login)
{
    if (ModelState.IsValid)
    {
        if (new AllFictionMembershipProvider().ValidateUser(login.Email, Criptografia.Encriptar(login.Senha)))
        {
            FormsAuthentication.SetAuthCookie(login.Email, false);

            return RedirectToDefault();

        }
    }

    ModelState.AddModelError("Erro", "Login ou senha incorretos!");
    return View(login);
}


I am thinking in creating a session that saves the id of the user after his login, because I need to access it in another controller, to make an insert. How Can I do it?
Posted

1 solution

When you are using this ActionResult, you are passing the Login Model, as in Username/Email and Password oviously. Out of these, atleast the Username/Email should be unique. So, out of these unique property, retreive the User Id from the database using the Unique property like

The context method LINQ would go like:
SQL
var userID = Context.USER.Where(user=>user.UserName == login.userName).select(user=>user.UserID);


Here I have considered, UserName as the Unique property, use the context method in the Service not in the controller.
Then Store the UserID in a session like
C#
Session["UserID"] = userID;
var idToBeUsedForInsert = Convert.ToInt32(Session["UserID"]);

Then try parse the Session into int and Then user the ID to insert into the database.
Hope you get some idea from this, else please post back your comments regarding your queries.
Thanks
:)
 
Share this answer
 
Comments
Member 11126363 15-Oct-14 13:04pm    
I think i got it. But how can I call it in another controller?
[no name] 15-Oct-14 13:06pm    
There is a very nice method, butbefore suggesting that I would like to know if your using repository pattern and dependency Injection ofcourse!!
Member 11126363 15-Oct-14 13:35pm    
Let me explain better. My table login has the following columns: IdLogin, IdUser, Email, password, and DateLogin. The Email and passoword, are the same that the user used to register on my site. On my db, I have a table called text that contains: IdText,IdUser, Texts). The user can write the text on my site, but they need to be logged on. So, When a user choose to write a text, I have to retrieve his Id, to put it in text table. So I will know who write that text.
[no name] 15-Oct-14 13:50pm    
This is pretty straight forward. Have you tried using the session and use that in the controller??
Member 11126363 15-Oct-14 14:01pm    
I tried to do with that example you posted earlier, but i'm not getting any Id.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900