繁体   English   中英

针对Azure AD的WebForms身份验证

[英]WebForms authentication against Azure AD

我有一个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并把整个事情移植过来,所以这条路线目前不是一个选择。

我对这个过程了解不多,所以如果我的问题没有意义,或者你需要更多信息,请告诉我,我很乐意尝试找到你需要的东西来帮助我。

也许我错过了一些东西,但我不明白为什么你需要自定义登录页面或外部登录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 auth只是为了避免每次请求都去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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM