Проверка подлинности OWIN Active Directory не работает должным образом - PullRequest
0 голосов
/ 16 апреля 2020

Привет, я включил Azure вход в активную директорию в моем приложении. Он не работает должным образом после перенаправления с портала Microsoft, не получая данные аутентифицированного пользователя.

Web.config

  <add key="owin:AppStartup" value="[NameSpace].Startup" />
  <add key="owin:AutomaticAppStartup" value="true"/>

Файл Startup.cs

[assembly: OwinStartup(typeof(NameSpace.Startup))]

namespace NameSpace
{
  public class Startup
  {
    string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];
    string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"];

// Tenant is the tenant ID (e.g. contoso.onmicrosoft.com, or 'common' for multi-tenant)
static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"];

string graphScopes = System.Configuration.ConfigurationManager.AppSettings["ida:GraphScopes"];
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
Scope = $"openid email profile offline_access {graphScopes}",

ResponseType = OpenIdConnectResponseType.IdToken,


TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = false
},
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = async (context) =>
{
var code = context.Code;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
string[] scopes = $"{graphScopes}".Split(new char[] { ' ' });


},
AuthenticationFailed = OnAuthenticationFailed
}
}
);

}

private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
{
context.HandleResponse();
context.Response.Redirect("/?errormessage=" + context.Exception.Message);
return Task.FromResult(0);
}
}
}

И действие входа в систему

public void SignIn()
        {
            if (!Request.IsAuthenticated)
            {
                // Signal OWIN to send an authorization request to Azure.
                HttpContext.GetOwinContext().Authentication.Challenge(
                  new AuthenticationProperties { RedirectUri = "/" },
                  OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }

        }

Проблема в том, что после перенаправления с портала Microsoft Request.IsAuthenticate имеет значение false. и его снова перенаправить на страницу входа.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...