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

  • SEARCH
  • Home
  • 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 8087631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:50:23+00:00 2026-06-05T18:50:23+00:00

I’m developing an MVC application and I have a routine that gets the currently

  • 0

I’m developing an MVC application and I have a routine that gets the currently logged on users password info and it works fine on my PC but when I publish my application to a live server on the domain, I don’t seem to be able to gain access to the AD information. I have used very similar code in a currently running asp.net web application and it works just fine. I compared security settings on both applications and they look identical. Here is the routine:

 public int GetPasswordExpiration()
    {
        PrincipalContext domain = new PrincipalContext(ContextType.Domain);
        string currUserName = WindowsIdentity.GetCurrent().Name;
        UserPrincipal currLogin = UserPrincipal.FindByIdentity(domain, currUserName);
        DateTime passwordLastSet = currLogin.LastPasswordSet.Value; //here is where it chokes***
        int doyPasswordSet = passwordLastSet.DayOfYear;
        int doy = DateTime.Today.DayOfYear;
        int daysSinceLastset = (doy - doyPasswordSet);
        int daysTilDue = (120 - daysSinceLastset);
        return (daysTilDue);

    }

I am an administrator on the domain so I think I have an application permissions issue, but since the failing application has the same permissions as the working application, I’m not sure where to look next. Any help is appreciated.

  • 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-05T18:50:24+00:00Added an answer on June 5, 2026 at 6:50 pm

    I’m answernig my own question because I want to post the code that works. Wiktor Zychla nailed it when asking if WindowsIdentity.GetCurrent().Name applied to the identity of the application pool rather than the logged in user. As a matter of fact it did, thanks Wiktor!

    Here is the modified code that works. I did change the way I got the users identity (explained why below).

    Controller Code:

    using MyProject.Utilities;  // Folder where I created the CommonFunctions.cs Class
    
    var cf = new CommonFunctions();
    string user = User.Identity.Name;
    ViewBag.PasswordExpires = cf.GetPasswordExpiration(user);
    

    Code in CommonFunctions

    public int GetPasswordExpiration(string user)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal currLogin = UserPrincipal.FindByIdentity(ctx, user);
            DateTime passwordLastSet = currLogin.LastPasswordSet.Value;
            int doyPasswordSet = passwordLastSet.DayOfYear;
            int doy = DateTime.Today.DayOfYear;
            int daysSinceLastset = (doy - doyPasswordSet);
            int daysTilDue = (120 - daysSinceLastset);
            return (daysTilDue);
    
        }
    

    The one thing that clued me in was that I decided to just run all the code in my controller and when I did, I got a red squiggly saying “The name WindowsIdentity does not exist in this context”:

    string currUserName = WindowsIdentity.GetCurrent().Name;

    Also, the reason I retrieved User.Identity.Name in the Controller and passed it to the function is because once I got things working and wanted to thin out my controller, I tried to get User.Identity.Name in the function but I got another red squiggly with the same message under User in this line:
    string user = User.Identity.Name;
    So I figure this is a .net thing and just went with getting the User.Identiy.Name in the controller, pass it to the function and all is well. This one really tested my patience and I hope this post can help someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.