繁体   English   中英

如何在ASP.NET MVC应用程序中单元测试“Azure AD和OpenID Connect”代码

[英]How to unit test 'Azure AD and OpenID Connect' code in ASP.NET MVC app

您好我使用Azure AD和OpenID Connect来验证我的应用程序的用户。 我使用了azure docs中提到的传统代码。

Startup.cs

public partial class Startup
{
    /// <summary>
    /// Function to set the authentication configurtion
    /// </summary>
    /// <param name="app">Owin properties</param>
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
    }
}

Startup.Auth.cs

private void ConfigureAuth(IAppBuilder app) {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions {
                ClientId = "ClientId",
                Authority = "Authority",
                PostLogoutRedirectUri = "PostLogoutRedirectUri"
            });
    }

AccountController.cs

public class AccountController : Controller
{
    public void SignIn()
    {
        // Send an OpenID Connect sign-in request.
        if (!Request.IsAuthenticated) {
            HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties {RedirectUri = "/"},
                OpenIdConnectAuthenticationDefaults.AuthenticationType);
        }
    }

    /// <summary>
    /// Function to signout the session user from the application
    /// </summary>
    public void SignOut()
    {
        if (Request.Url == null) {
            return;
        }
        string callbackUrl = Url.Action("SignOutCallback", "Account", null, Request.Url.Scheme);

        HttpContext.GetOwinContext().Authentication.SignOut(
            new AuthenticationProperties {RedirectUri = callbackUrl},
            OpenIdConnectAuthenticationDefaults.AuthenticationType,
            CookieAuthenticationDefaults.AuthenticationType);
    }

    /// <summary>
    /// Function to redirect the user to home screen.
    /// </summary>
    /// <returns>Redirects to home screen if authentication still exists</returns>
    public ActionResult SignOutCallback()
    {
        if (Request.IsAuthenticated) {
            return RedirectToAction("Index", "Home");
        }

        return View();
    }
}

不知道如何开始单元测试上面的代码。 我没有找到关于这个主题的有用资源,会欣赏任何线索。

@ coder89

在我看来,单元测试不是一种选择。 作为框架一部分的所有类都已经过单元测试。 从您的问题来看,在我看来,您正在寻找的是某种集成测试。 当您在这里运行整个管道时,您可能正在使用像Selenium之类的东西来进行UI测试。 Browserstack广泛用于此类测试。

暂无
暂无

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

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