locked
Problems with Windows authentication RRS feed

  • Question

  • User-1634117515 posted

    Hello,

    Im using this to show the name of the user 

     protected void Page_Load(object sender, EventArgs e)
        {
         txtNombre.Text = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
        }

    but when I published it I get the error "Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'."

    The server has already the windows authentification activated and the web.config is 

    <authentication mode="Windows"/>

    how Can I solve this?

    Wednesday, June 12, 2019 5:27 PM

Answers

  • User475983607 posted

    It looks like you've configured OWIN Identity?  You'll need to turn off the Cookie Authentication if you wish to use Windows Authentication.

    On a side this, Word Interop...

    <add assembly="Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>

    ... is not recommend for use on a web server.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 6:57 PM

All replies

  • User475983607 posted

    The correct construct is for a web application is.

    HttpContext.Current.User.Identity.Name

    Wednesday, June 12, 2019 5:42 PM
  • User-1634117515 posted

    Thanks, but it returns a null value. and I need the full name and the users from the AD

    Wednesday, June 12, 2019 5:48 PM
  • User475983607 posted

    markdirtyboy

    Thanks, but it returns a null value. and I need the full name and the users from the AD

    Usually indicates anonymous authentication is enabled in IIS.  Disable Anonymous Authentication.

    Wednesday, June 12, 2019 5:50 PM
  • User-1634117515 posted

    I activated Windows authentication and disabled anonymous and I get this page 

    HTTP Error 404.15 - Not Found
    The request filtering module is configured to deny a request where the query string is too long.
    
    Most likely causes:
    Request filtering is configured on the Web server to deny the request because the query string is too long.
    
    Things you can try:
    Verify the configuration/system.webServer/security/requestFiltering/requestLimits@maxQueryString setting in the applicationhost.config or web.config file.
    
    Detailed Error Information:
    Module	   RequestFilteringModule
    Notification	   BeginRequest
    Handler	   ExtensionlessUrl-Integrated-4.0
    Error Code	   0x00000000
    Requested URL	   http://localhost:51182/Account/Login?ReturnUrl=%2FAccount%2FLogin%3FReturnUrl%3D%252FAccount%252FLogin%253FReturnUrl%253D%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D%252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525252FAccount%252525252525252FLogin%252525252525253FReturnUrl%252525252525253D%25252525252525252FAccount%25252525252525252FLogin%25252525252525253FReturnUrl%25252525252525253D%2525252525252525252FAccount%2525252525252525252FLogin%2525252525252525253FReturnUrl%2525252525252525253D%252525252525252525252FAccount%252525252525252525252FLogin%252525252525252525253FReturnUrl%252525252525252525253D%25252525252525252525252FAccount%25252525252525252525252FLogin%25252525252525252525253FReturnUrl%25252525252525252525253D%2525252525252525252525252FAccount%2525252525252525252525252FLogin%2525252525252525252525253FReturnUrl%2525252525252525252525253D%252525252525252525252525252FAccount%252525252525252525252525252FLogin%252525252525252525252525253FReturnUrl%252525252525252525252525253D%25252525252525252525252525252FAccount%25252525252525252525252525252FLogin%25252525252525252525252525253FReturnUrl%25252525252525252525252525253D%2525252525252525252525252525252FAccount%2525252525252525252525252525252FLogin%2525252525252525252525252525253FReturnUrl%2525252525252525252525252525253D%252525252525252525252525252525252FAccount%252525252525252525252525252525252FLogin%252525252525252525252525252525253FReturnUrl%252525252525252525252525252525253D%25252525252525252525252525252525252FAccount%25252525252525252525252525252525252FLogin%25252525252525252525252525252525253FReturnUrl%25252525252525252525252525252525253D%2525252525252525252525252525252525252FAccount%2525252525252525252525252525252525252FLogin%2525252525252525252525252525252525253FReturnUrl%2525252525252525252525252525252525253D%252525252525252525252525252525252525252FDefault
    Physical Path	   J:\Intranet\Intranet\Account\Login
    Logon Method	   Not yet determined
    Logon User	   Not yet determined
    Request Tracing Directory	   C:\Users\msuarez\Documents\IISExpress\TraceLogFiles\INTRANET

    Wednesday, June 12, 2019 5:55 PM
  • User475983607 posted

    markdirtyboy

    I activated Windows authentication and disabled anonymous and I get this page 

    HTTP Error 404.15 - Not Found
    The request filtering module is configured to deny a request where the query string is too long.
    
    Most likely causes:
    Request filtering is configured on the Web server to deny the request because the query string is too long.
    
    Things you can try:
    Verify the configuration/system.webServer/security/requestFiltering/requestLimits@maxQueryString setting in the applicationhost.config or web.config file.
    
    Detailed Error Information:
    Module	   RequestFilteringModule
    Notification	   BeginRequest
    Handler	   ExtensionlessUrl-Integrated-4.0
    Error Code	   0x00000000
    Requested URL	   http://localhost:51182/Account/Login?ReturnUrl=%2FAccount%2FLogin%3FReturnUrl%3D%252FAccount%252FLogin%253FReturnUrl%253D%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D%252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525252FAccount%252525252525252FLogin%252525252525253FReturnUrl%252525252525253D%25252525252525252FAccount%25252525252525252FLogin%25252525252525253FReturnUrl%25252525252525253D%2525252525252525252FAccount%2525252525252525252FLogin%2525252525252525253FReturnUrl%2525252525252525253D%252525252525252525252FAccount%252525252525252525252FLogin%252525252525252525253FReturnUrl%252525252525252525253D%25252525252525252525252FAccount%25252525252525252525252FLogin%25252525252525252525253FReturnUrl%25252525252525252525253D%2525252525252525252525252FAccount%2525252525252525252525252FLogin%2525252525252525252525253FReturnUrl%2525252525252525252525253D%252525252525252525252525252FAccount%252525252525252525252525252FLogin%252525252525252525252525253FReturnUrl%252525252525252525252525253D%25252525252525252525252525252FAccount%25252525252525252525252525252FLogin%25252525252525252525252525253FReturnUrl%25252525252525252525252525253D%2525252525252525252525252525252FAccount%2525252525252525252525252525252FLogin%2525252525252525252525252525253FReturnUrl%2525252525252525252525252525253D%252525252525252525252525252525252FAccount%252525252525252525252525252525252FLogin%252525252525252525252525252525253FReturnUrl%252525252525252525252525252525253D%25252525252525252525252525252525252FAccount%25252525252525252525252525252525252FLogin%25252525252525252525252525252525253FReturnUrl%25252525252525252525252525252525253D%2525252525252525252525252525252525252FAccount%2525252525252525252525252525252525252FLogin%2525252525252525252525252525252525253FReturnUrl%2525252525252525252525252525252525253D%252525252525252525252525252525252525252FDefault
    Physical Path	   J:\Intranet\Intranet\Account\Login
    Logon Method	   Not yet determined
    Logon User	   Not yet determined
    Request Tracing Directory	   C:\Users\msuarez\Documents\IISExpress\TraceLogFiles\INTRANET

    Usually caused by a redirect to a login page configured in the web.config.  Windows authentication does not require a login page.  IIS (and the browser) handle the user authentication behind the scenes. 

    What are you trying to do exactly? 

    Wednesday, June 12, 2019 6:03 PM
  • User-1634117515 posted

    I don´t have any login Page, how Can I see this? 

    and I´m developing an Intranet so the page must get the name of the user for their requirements 

    Wednesday, June 12, 2019 6:07 PM
  • User475983607 posted

    markdirtyboy

    I don´t have any login Page, how Can I see this? 

    and I´m developing an Intranet so the page must get the name of the user for their requirements 

    Look in the web.config and/or make sure Forms Authentication is disabled in IIS.

    If that does not work try adding the following to the web.config.

    <system.webServer>
     <modules runAllManagedModulesForAllRequests="true>
        <remove name="FormsAuthentication />
     </modules>
    </system.webServer>

    Wednesday, June 12, 2019 6:16 PM
  • User-1634117515 posted

    Here is my Web.config

    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
      </configSections>
      <system.web>
        <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="es-ES" uiCulture="de-DE"/>
        <customErrors mode="Off" defaultRedirect="Default.aspx"/>
        <authentication mode="Windows"/>
        <authorization>
          <allow users="*"/>
        </authorization>
        <identity impersonate="false"/>
        <compilation debug="true" targetFramework="4.7">
          <assemblies>
            <add assembly="Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
            <add assembly="System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
          </assemblies>
        </compilation>
        <httpRuntime targetFramework="4.6"/>
        <pages>
          <namespaces>
            <add namespace="System.Web.Optimization"/>
            <add namespace="Microsoft.AspNet.Identity"/>
          </namespaces>
          <controls>
            <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
          </controls>
        </pages>
        <sessionState mode="InProc" customProvider="DefaultSessionProvider">
          <providers>
            <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"/>
          </providers>
        </sessionState>
      </system.web>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <remove name="FormsAuthentication"/>
        </modules>
      </system.webServer>
    </configuration>

    and I still get that :c

    Wednesday, June 12, 2019 6:22 PM
  • User475983607 posted

    It looks like you've configured OWIN Identity?  You'll need to turn off the Cookie Authentication if you wish to use Windows Authentication.

    On a side this, Word Interop...

    <add assembly="Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>

    ... is not recommend for use on a web server.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 12, 2019 6:57 PM