2

I am having a problem I can not find a tutorial or some code where I am able to do some session state. For example I would like to create a application where when a user logs in, he or she can only view there information for example a student viewing his grades an no other student can see that information. I have achieved this in VB 2008 last year but need help in MVC3 as it is not the same as the language as i am using C#. In vb 2008 i achieved this connecting a table from the ASP.net database (users) and joined to my employee table by a foreign key. And added the following code:

  Session("ID") = objUser
    Dim db As New DataClassesDataContext
    Dim info = From a In db.tblCourses _
               Where a.CourseTitle = ddlCourseName.SelectedItem.Value _
               Select a.CourseId Order By CourseId Descending
    crseID = info.FirstOrDefault()

    Session("Course") = crseID
    sdsAddStudent.Insert()

    FName.Text = ""
    LName.Text = ""
    Address.Text = ""
    ddlCourseName.SelectedIndex = 0
    Session("UserID2") = objUser
    Session("RoleID2") = "f13b9bf3-593d-4170-bfaa-bc43655773e2"
    sdsRoleStudent.Insert()

I know VB is different to C# MVC3 as this is 2008 and not MVC3 am just showing this code so people know that I am not after free code and have tried to make an effort which has not succeed.

Thank You For Your Kind Help People

1 Answer 1

1

I'm not clear what exactly are you trying to achieve.

If you want to limit users access exclusively to the data that belongs to them, you need to have this data related their IDs in Users table. There is very little that you have told about table structure, but I think it is safe to assume that you have Courses table and something like UsersInCourses that would map many users to a single Course. All you would have to do is to select courses have User's Id assigned to them. To get currently logged in user you just have refer to

User.Identity.Name

in your application's code.

So let's assume your class is cooking reciepe :

public class Recipe
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string PreparationInstructions { get; set; }
    public DateTime CreationDate { get; set; }
}

and you want to have an Action that returns recipes for currently logged in user

    public ActionResult ShowMyRecipes()
    {
        var myRecipes = dbContext.Recipes.Where(recipe => recipe.Author.Equals(User.Identity.Name)).ToList();
        return View(myRecipes);
    }


    public ActionResult CreateRecipe(Recipe recipe)
    {
        // set Author to curently logged in user's key
        recipe.Author = Membership.GetUser().ProviderUserKey;
        // save changes
        dbContext.Recipes.Add(recipe);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the reply your assumption for my tables is correct but with this code will the user have access to his information only.
I'm not sure if I follow your logic, but take a look at my updated response. If you're finding it hard to get used to C# in ASP.NET MVC context, I would advise taking some time to review great tutorial that's available as PDF on mvcmusicstore.codeplex.com this should answer most of your questions as it describes mechanims and provides good, clear code
In the application there is a ASP.net generated database where all users are stored do I have to connect the UserID pk in my table for example your cooking recipe if there is a table users you have created (authors) would I need to link the ID from the asp.net table users to mine. Thanks
You don't have to, however if you do it will make things easier and more error-proof. If you don't wan't - you can just get currently logged in users key Membership.GetUser().ProviderUserKey (see updated answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.