简体   繁体   English

针对Azure AD的WebForms身份验证

[英]WebForms authentication against Azure AD

I have a WebForms site that has been running on an internal server and authenticating users against our internal Active Directory. 我有一个WebForms站点,它已在内部服务器上运行,并根据我们的内部Active Directory对用户进行身份验证。 Due to some new features that we are implementing, this site needs to be moved to an external server and then authentication changed so that it authenticates users against our Office 365 accounts. 由于我们正在实施一些新功能,因此需要将此站点移动到外部服务器,然后更改身份验证,以便根据我们的Office 365帐户对用户进行身份验证。 To this end I have: 为此,我有:

  1. Created a new WebForms site (not using MVC) 创建了一个新的WebForms站点(不使用MVC)
  2. Set up a new application in Azure. 在Azure中设置新应用程序。
  3. Modified the Startup.Auth.cs as follows: 修改了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" }); 

When I go to the default page and click Log On, it takes me to the correct Login page and the button for OpenID is shown. 当我转到默认页面并单击“登录”时,它会转到正确的“登录”页面,并显示OpenID按钮。 If I click the button, I am taken to the Microsoft Login page where I am able to enter my credentials. 如果我单击该按钮,我将进入Microsoft登录页面,在那里我可以输入我的凭据。 However, at that point, I am redirected back to my site's login page where it is still asking for a username/password. 但是,在那时,我被重定向回我网站的登录页面,在那里仍然要求输入用户名/密码。

What I would like to have happen is to set the site up so that if a user is not authenticated, they are redirected directly to the Microsoft login page and upon successful login are redirected back to the page they requested originally. 我希望发生的是设置站点,以便在未对用户进行身份验证时,将其直接重定向到Microsoft登录页面,并在成功登录后重定向回他们最初请求的页面。 Failing this, I would be satisfied with getting the default login page working so that when I click OpenID I'm not redirected back to the login page. 如果做不到这一点,我会对使默认登录页面工作感到满意,这样当我点击OpenID时,我没有重定向回登录页面。

I don't have time to learn MVC at this point and port the whole thing over so going that route is not an option at this time. 我没有时间在这一点上学习MVC并把整个事情移植过来,所以这条路线目前不是一个选择。

I don't know enough about this process, so if my question doesn't make sense or if you need more information, please let me know and I'll be glad to try and find what you need to assist me in this. 我对这个过程了解不多,所以如果我的问题没有意义,或者你需要更多信息,请告诉我,我很乐意尝试找到你需要的东西来帮助我。

Maybe I'm missing something, but I don't see why you need the custom login page or the external signin cookie. 也许我错过了一些东西,但我不明白为什么你需要自定义登录页面或外部登录cookie。 A typical Startup.Auth for OIDC/AAD looks something like this: 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);
                }
        }
    });

The cookie auth is just to keep from going to AAD for every single request. cookie auth只是为了避免每次请求都去AAD。 All the real work happens in the OpenIdConnectAuthentication. 所有实际工作都发生在OpenIdConnectAuthentication中。

Here's an example of WebForms, Azure AD, and OpenID Connect: 以下是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/ 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