【问题标题】:Modify Startup.cs code from Core to legacy ASP.NET将 Startup.cs 代码从 Core 修改为旧版 ASP.NET
【发布时间】:2021-11-10 16:03:54
【问题描述】:

我想修改旧的 ASP.NET WebForms 应用程序以使用 Azure SAML 身份验证。我在 Microsoft 站点上找到了 ASP.NET Core 的 SAML 身份验证示例,并希望修改此代码以适合我的 Web 窗体应用程序启动类。

下面我要修改的代码:

services.AddAuthentication(sharedOptions =>
{
  sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  sharedOptions.DefaultChallengeScheme = "Saml2";
})
.AddSaml2(options =>
{
  options.SPOptions.EntityId = new EntityId("https://localhost:44342/Saml2");
  options.IdentityProviders.Add(
    new IdentityProvider(
      new EntityId("https://sts.windows.net/63eb1bcb-f74f-4703-8243-6f73d78ebf52/"), options.SPOptions)
      {
        MetadataLocation = "https://login.microsoftonline.com/63eb1bcb-f74f-4703-8243-6f73d78ebf52/federationmetadata/2007-06/federationmetadata.xml?appid=9fd05134-d507-479b-a432-580541125356"
      });
  })
.AddCookie();

此代码使用我的启动类中不可用的“服务”。我现有的代码如下所示:

  public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            //interactive logon process
            AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
            //name of the authentication type
            AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
            //Login path should be below
            // LoginPath = new PathString("login"),
            //TODO: Enable this to always send and receive cookies in SSL when in production
            CookieSecure = CookieSecureOption.Always,
            //enable sliding expiration
            SlidingExpiration = true,
            //Cookie expires in 4 hours
            ExpireTimeSpan = TimeSpan.FromTicks(DateTime.Now.AddHours(4).Ticks)

        });
        Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;

        app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
        {

            MetadataAddress = adfsMetadata,
            Wtrealm = realm
        });

        //This will set ADFS as the default authentication provider 
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseStageMarker(PipelineStage.Authenticate);
    }

【问题讨论】:

  • 只是出于兴趣,您发现了什么 Microsoft SAML 样本?
  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…
  • @JasonPan 当然我会的。我正在检查链接。
  • @rbrayb 我没有找到任何适用于 ASP.NET WebForms 的 Microsoft 示例
  • 好的 - 当您说“Microsoft 站点上的 SAML 身份验证示例”时,我很困惑,因为没有 Microsoft SAML 示例。这里有一组客户端堆栈 - medium.com/the-new-control-plane/…

标签: asp.net azure asp.net-core webforms saml


【解决方案1】:

更新

下面的文章应该比上一篇简单。你可以参考一下。

Code Your C#/ASP.NET App to Provide SSO via OneLogin

示例代码:https://github.com/onelogin/dotnet-saml

上一页

您可以参考此文档了解如何在 Startup.Auth.cs 类中安装您的 Web 表单应用程序。

官方文档:

Sustainsys.Saml2 --ASP.NET Web Forms

您也可以参考此代码。

SampleOwinApplication

【讨论】:

  • 当我尝试安装 Nuget 时,我收到错误消息:“无法安装包 'Sustainsys.Saml2.HttpModule 2.8.0'。您正在尝试将此包安装到以 ' 为目标的项目中.NETFramework,Version=v4.6.1',但该包不包含任何与该框架兼容的程序集引用或内容文件。” .... 它期望哪个框架?
  • 我整理的其他问题。我在这一行收到错误:app.CreatePerOwinContext(ApplicationDbContext.Create); .找不到:ApplicationDbContext
  • @RKh 我已经更新了我的答案,希望对你有用。
  • 谢谢。我正在检查您提供的新链接。
猜你喜欢
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多