Привет! Я использую Azure AD и OpenID Connect для аутентификации пользователей в моем приложении. Я использовал традиционный код, упомянутый в документах Azure.
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();
}
}
Не уверен, как начать модульное тестирование приведенного выше кода. Я не нашел много полезных ресурсов по этой теме, оценит любые предложения.