Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day.

I have a MVC5 project using identity 2. I created a table named branch which stores 5 branch locations. The goal is to have the user select the branch he wants to log into, this will then be tagged to all their transactions. Once logged in the user will only be able to see transaction(theirs and others) corresponding to the branch he selected after initial authentication.


Could you point me to the right the resources that could be of benefit for this. Thanks

What I have tried:

I have extended the account model to include a foreign fey for a table called branch lookup this will then provide a many to many relationship to the table branches
Posted
Updated 1-Dec-18 19:03pm

1 solution

So you've already tied to the user to the branch when they get registered into the User table that gets created in the identity framework right?

When you get a user (assuming you are using entity framework) you should be able to get the transaction id by an EF select statement (var user = users.Where(m=>m.username == "").select(m=>m.transactions);).

But to the main point of your issue.

I think, whatever data you are storing, you'll need to include the transaction guid/id from the user in the row being saved to the database.

To store the data, you could create an extension method on the IIdentity interface to get access to the transaction id tied to that user. Something like this

C#
public static string GetTransactionId(this IIdentity identity)
        {
            var username = identity.GetUserName();
            var users = userstable.Where(m => m.UserName == username).Select(m => m.Transactions).FirstOrDefault;
            
            if (user == null)
            {
                return "";
            }

            var transId = users.TransactionId;
            return transId;
        }


Then the usage would simply be

C#
User.Identity.GetTransactionId()


You'd be able to use this to get the transaction id and then save it to the table.

From there, in your controller/models you'd get the data from your DB by passing in the transcation id from the controller.

Something like

C#
var transactionId = User.Identity.GetTransactionId();
var model = new MyDataModel();
model.Build(transactionId);


Then within your model you'd do something along the lines of

C#
var mydatainmodel = tableindb.Where(m=>m.TransactionId == transactionIdPassedIn);


I may have mixed terminology between what you called "branch" and "transaction" but hopefully this steers you in the right direction.
 
Share this answer
 

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