Question

First off, let's start by saying I'm a rookie. That being said, I am building a web app in C# using MVC3 w/ EF4.3.1 using a db first approach. During the log on process, I want to set some session variables to be used throughout the app.

My issue is that I am not sure how to go about reading the values into the session variables. Below is the code currently in the AccountController.cs:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, 
                                              model.RememberMe);
            if (Url.IsLocalUrl(returnUrl) 
                && returnUrl.Length > 1 
                && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") 
                && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", 
                "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

And from what I have read, I should be trying to use DbContext to access the property of a given field, similar to:

using (var context = new DbContext())
{
    var svc100 = context.SVC00100.Where(t => 
                        t.TECHEMAIL.Contains("model.UserName")

    // Read the current value of the Name property
    Session["Name"] = context.Entry(svc100).Property(u =>
                                                 u.Name).CurrentValue;
}

Now... How do I determine what should be used in place of DbContext() in the second code block? If I try to use the drop down and pick what I need, I do not see anything relating to my Entity (CPLUEntities) that contains the DbContext phrase.

Some of the links I've discovered:

Implement custom “ValidateUser” in MembershipProvider

Entity Framework Working with Property Values

Using DbContext in EF 4.1 Part 5: Working with Property Values

Was it helpful?

Solution

using (var context = new DbContext())
  {
     var svc100 = context.SVC00100
                 .FirstOrDefault(t => t.TECHEMAIL.Contains(model.UserName));

     // Read the current value of the Name property
     if (svc100 != null && !String.IsNullOrEmpty(svc100.Name))
         Session["Name"] = svc100.Name;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top