【问题标题】:WebForms authentication against Azure AD针对 Azure AD 的 WebForms 身份验证
【发布时间】:2015-05-13 12:19:13
【问题描述】:

我有一个在内部服务器上运行的 WebForms 站点,并针对我们的内部 Active Directory 对用户进行身份验证。由于我们正在实施的一些新功能,需要将此站点移动到外部服务器,然后更改身份验证,以便根据我们的 Office 365 帐户对用户进行身份验证。为此我有:

  1. 创建了一个新的 WebForms 站点(不使用 MVC)
  2. 在 Azure 中设置新应用程序。
  3. 修改Startup.Auth.cs如下:

        public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });
    
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions { ClientId = "MyApplicationGUID", Authority = "https://login.windows.net/MyDomain.com" });
    

当我转到默认页面并单击登录时,它会将我带到正确的登录页面并显示 OpenID 按钮。如果我单击该按钮,我将进入 Microsoft 登录页面,我可以在其中输入我的凭据。但是,此时,我被重定向回我的网站的登录页面,它仍然要求输入用户名/密码。

我想要做的是设置站点,以便如果用户未通过身份验证,他们将直接重定向到 Microsoft 登录页面,并在成功登录后重定向回他们最初请求的页面。如果不这样做,我会对让默认登录页面正常工作感到满意,这样当我单击 OpenID 时,我不会被重定向回登录页面。

我现在没有时间学习 MVC 并将整个事情移植过来,所以目前不能选择这条路线。

我对这个过程知之甚少,所以如果我的问题没有意义或您需要更多信息,请告诉我,我很乐意尝试找到您需要的帮助我这个。

【问题讨论】:

    标签: c# asp.net authentication azure webforms


    【解决方案1】:

    也许我遗漏了一些东西,但我不明白您为什么需要自定义登录页面或外部登录 cookie。 OIDC/AAD 的典型 Startup.Auth 如下所示:

    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
    
    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = "AppGUID",
            Authority = "https://login.windows.net/MyDomain.com",
    
            // After authentication return user to the page they were trying
            // to access before being redirected to the Azure AD signin page.
            Notifications = new OpenIdConnectAuthenticationNotifications()
            {
                RedirectToIdentityProvider = (context) =>
                    {
                        string currentUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.Path;
                        context.ProtocolMessage.RedirectUri = currentUrl;
    
                        return Task.FromResult(0);
                    }
            }
        });
    

    cookie 身份验证只是为了避免每次请求都进入 AAD。所有真正的工作都发生在 OpenIdConnectAuthentication 中。

    以下是 WebForms、Azure AD 和 OpenID Connect 的示例:

    http://www.cloudidentity.com/blog/2014/07/24/protecting-an-asp-net-webforms-app-with-openid-connect-and-azure-ad/

    【讨论】:

    • 谢谢。这让我准确地到达了我需要的地方。
    猜你喜欢
    • 2020-10-14
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2019-06-25
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多