如何从 C# MVC 中的 CAS 服务器检索用户名

How can I retrieve the username from a CAS server in c# MVC

我搜索了这些板,虽然我找到了有关 CAS(中央身份验证服务)的信息,但我没有找到任何关于如何在用户名被重定向回后从 CAS 服务器检索用户名的信息客户端应用程序。

在使用 dotNetCasClient.dll 时,我已按照 GitHub 上的步骤配置我的 Web.config 文件的指南。下面是 web.config 代码:(注意:出于隐私原因,我不得不替换服务器名称)

    ?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <!-- CAS Configuration -->
  <configSections>
        <section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration, DotNetCasClient" />
    </configSections>

  <casClientConfig casServerLoginUrl="https://mycasserver/login"
                   casServerUrlPrefix="https://mycasserver"
                   serverName="https://myappserver"
                   notAuthorizedUrl="~/Home"
                   cookiesRequiredUrl="~/Home/CookiesRequired"
                   redirectAfterValidation="true"
                   gateway="false"
                   renew="false"
                   singleSignOut="true"
                   ticketTimeTolerance="5000"
                   ticketValidatorName="Cas20"
                   serviceTicketManager="CacheServiceTicketManager"
                   gatewayStatusCookieName="CasGatewayStatus" />

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <!--<modules runAllManagedModulesForAllRequests="true">-->
    <modules>
          <remove name="DotNetCasClient" />
      <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient" />
    </modules>
  </system.webServer>
  <!-- /CAS Configuration -->


  <connectionStrings configSource="connectionStrings.config" />
    <system.data>
      <DbProviderFactories>
        <remove invariant="Oracle.DataAccess.Client" />
        <!-- If any should be in the machine.config -->
        <add name="Oracle Data Provider for .NET" 
             invariant="Oracle.DataAccess.Client" 
             description="Oracle Data Provider for .NET" 
             type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
      </DbProviderFactories>
    </system.data>

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />


    <!-- CAS Configuration -->
    <add key="enableSimpleMembership" value="false" />
    <add key="autoFormsAuthentication" value="false" />
    <!-- /CAS Configuration -->

  </appSettings>

  <system.web>
  <sessionState mode="StateServer" />
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />

  <!-- CAS Configuration -->

  <authentication mode="Forms">
        <forms loginUrl="https://mycasserver/login" 
               cookieless="UseCookies"
               path="https://myappserver" />
  </authentication>

  <httpModules>
  <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient" />
  </httpModules>  

  <!-- /CAS Configuration -->

</system.web>


  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" 
                extension=".cs" 
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
                warningLevel="4" 
                compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />

      <compiler language="vb;vbs;visualbasic;vbscript" 
                extension=".vb" 
                type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
                warningLevel="4" 
                compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />

    </compilers>
  </system.codedom>
</configuration>

根据有关 CAS 客户端的说明,我还在登录页面上方放置了授权标签,这样当用户想要登录时,他们会被重定向到 CAS 服务器登录,并且在成功验证后, "Login" ActionResult 将用户重定向到特定页面。

    [Authorize]
    public ActionResult Login()
    {
        return RedirectToAction("Index", "Departments");
    }

我已经成功测试了 CAS 实现并可以确认它按预期工作,但是我完全不知道在验证用户后我将如何从 CAS 服务器获取信息。

感谢您提供任何帮助,如果这是重复的 post,我深表歉意,因为我自己搜索时无法找到此类内容。

看起来可能是从HttpContext.Current.User.Identity.Name中提取值的问题,这应该return已验证的CAS用户名的值用户。 jasig-cas-user mailing list 上有一个 post 让我知道了这一点。

Another mailing list post 表明其他 returned 的信息可以通过 CasAuthentication.CurrentPrincipal.Assertion.Attributes 访问,但没有特别详细的记录在他们的维基上。

这意味着从 CAS return 访问数据在与代码一起分发的示例 Web 应用程序中进行了演示,但它不会很直观地存在并且没有在其他任何地方记录。 (我使用 NuGet 安装,完全错过了示例应用程序。在我自己找到答案之前,我实际上已经找到了你的问题!)