0

So I'm working on an Activity Input page where the user inputs his hours put in depending on the projects of the company I'm currently working in as an Intern.

Here's the catch though, so far the display of projects names (depending on the user of course) only works when I put directly my name - so I was wondering if there was a way to store the name of the currently logged in user? (In the source code below, "kade" represents my name, and that's what I wish to replace)

IEnumerable<SelectListItem> cb_listprojects = pr.ListProject("kade", strYear, strMonth);  // here "kade" represents my name, same for the rest of the source code
        List<IEnumerable<SelectListItem>> ddlists = new List<IEnumerable<SelectListItem>>();

        foreach (string s in ViewBag.ListDaysOfMonth)
            ddlists.Add(_service.ListHours(0));

        ViewBag.ddlists = ddlists;

        IEnumerable<Project> lp = _service.List("kade", strYear, strMonth);
        UserActivityDb db = new UserActivityDb(@"metadata=res://*/Models.CRA.csdl|res://*/Models.CRA.ssdl|res://*/Models.CRA.msl;provider=Microsoft OLE DB Provider for SQL server;provider connection string=&quot;data source=(local)\SQLEXPRESS;initial catalog=CRAV34;persist security info=True;user id=sa;password=as0;multipleactiveresultsets=True;App=EntityFramework&quot;");
        lp = lp.Where(p => (true == db.ExistUserActivity(int.Parse(strYear), int.Parse(strMonth), ViewBag.ListDaysOfMonth, p.ProjectId, "kade")));


        //nea61
        foreach (Project p in lp)
        {
            cb_listprojects = cb_listprojects.Where(i => (i.Value != p.ProjectId.ToString())).ToList();                                       
        }
        ViewBag.ProjectList = cb_listprojects;

        //nea36
        List<string[]> lstInts = new List<string[]>();

        foreach (Project p in lp)
        {
            string strInts = db.getFormattedData(int.Parse(strYear), int.Parse(strMonth), ViewBag.ListDaysOfMonth, p.ProjectId, "kade", null, 0);
            string[] ints = strInts.Split(';');

            lstInts.Add(ints);
        }
        ViewBag.ProjectInts = lstInts;            

        return View(lp);

So far I have tried HttpContext.Current.User.Identity.Name and it is empty once I enter ActivityInputCreate in the Controller.

Any other ideas? Thanks.

EDIT : string strUser = Environment.UserName.ToLower() is the best option I found so far - for example my UserName being KADE it'll work in this case; problem is if the user has a random or a different username then his log-in (which are all their last name).

Christopher
  • 1,712
  • 2
  • 21
  • 50

1 Answers1

1

Add the following name spaces to the controller where your code is placed. I actually think only the first line should be enough:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

and then in the method, you can use the following piece to get the username:

var uname = User.Identity.GetUserName();
Behrooz
  • 1,895
  • 4
  • 31
  • 47