Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:42:00+00:00 2026-06-18T01:42:00+00:00

First, I’m sad to say I’m not sure whether this code should be in

  • 0

First, I’m sad to say I’m not sure whether this code should be in the _Layout.cshtml or somewhere in the controller. It needs to run on all pages, so I’ve put it in the _Layout.cshtml page.

This is for an intranet web app. What I’m attempting to do is this: if a cookie (holding the user’s userid) is not found, get the windows username, run it through a class that will go into the database and get the corresponding user’s username, and – if we get a user id back – make a cookie containing it. Doesn’t sound too hard, but one line in particular, and various incarnations of it, is refusing to be supportive. Here’s the code as a whole.

        if(!Context.Response.Cookies.AllKeys.Contains("userid")){
            var winuser = System.Web.HttpContext.Current.User.Identity.Name;
            var winuserid = myprojectname.Models.MyIntranetDataContext.getUserId(winuser).UserID();
            if (winuserid == null) {
                Response.Redirect("/someotherpage");
            } else {
                HttpCookie cookieuser = new HttpCookie("userid");
                DateTime now = DateTime.Now;
                cookieuser.Value = winuserid;
                cookieuser.Expires = now.AddMonths(1);

                Response.Cookies.Add(cookieuser);
            }
        }

Line 2 – var winuser… – appears to be the problem. In this current incarnation, I’m getting a build error: An object reference is required for the non-static field, method, or property ‘myprojectname.Models.MyIntranetDataContext.getUserId(string)’

It doesn’t like it when I add a .ToString to it either.

I’ve tried making winuser this as well:
Page.User.Identity.Name;
That gave no build errors. When I attempt to Start Debugging, she blows up with this beauty of an error: ‘Cannot perform runtime binding on a null reference’

Once I get the windows username, all will be well.

Really, this isn’t about cookies, or even mvc to much of an extent (except maybe guidance on where to put this code – the _Layout.cshtml?). Really it’s about getting the windows username, which I seem unable to do. Thanks in advance for any assistance you are able to provide.

Note, the above names aren’t actual – just for example only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:42:01+00:00Added an answer on June 18, 2026 at 1:42 am

    If they are on the domain, couldn’t you use something like the following to retrieve that information?

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    
    WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
    WindowsIdentity identity = (WindowsIdentity)principal.Identity;
    
    String userName= principal.Identity.Name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, this has never worked before, so it is not specifically related to VS
First of all, this code , as ugly as it is, works, for all
First the code (sorry if its not 100%) I am no expert and then
First method $start = microtime(true); // code $end = microtime(true); echo 'This page loaded
First, please tell me if I'm not allowed to ask about this protocol here...
First of all, this isn't for a keylogger, it's for an input in a
first take a look on this picture from localScope app : i have 2
First you should know I have looked into many questions and none of them
First off, my excuse for posting here, normally I feel I should be able
First I want to say you that I'm totally new in Xcode so I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.