簡體   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