如何在同一个应用程序中配置表单身份验证和azure身份验证?

时间:2017-12-08 11:12:11

标签: asp.net-mvc azure login oauth office365

我想在我的应用程序中使用表单login和azure登录(office 365登录)。我在asp.net mvc。

工作

我们可以使用表格登录或Office 365登录登录。

我正在使用OAuth身份验证(owin)进行Office 365登录。

我正在使用以下代码:

启动:

public void ConfigureAuth(IAppBuilder app)
{
   app.UseKentorOwinCookieSaver();

   app.SetDefaultSignInAsAuthenticationType
   (CookieAuthenticationDefaults.AuthenticationType);

    var cookieAuthentication = new CookieAuthenticationOptions
    {
      LoginPath = new PathString("/login"),
      CookieSecure = CookieSecureOption.Always,
      ExpireTimeSpan = new TimeSpan(0, 7200, 0),
      SlidingExpiration = true
    };


   app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
  {
    ClientId = clientId,
    Authority = authority,
    PostLogoutRedirectUri = postLogoutRedirectUri,
    RedirectUri = postLogoutRedirectUri,
    UseTokenLifetime = false,
    Notifications = new OpenIdConnectAuthenticationNotifications
    {
       AuthenticationFailed = context =>
       {
        if (context.Exception.Message.StartsWith("OICE_20004") ||             
           context.Exception.Message.Contains("IDX10311"))
         {
             context.SkipToNextMiddleware();
             context.Response.Redirect("/logout");
             return Task.FromResult(0);
          }

           return Task.FromResult(0);
       },

       SecurityTokenValidated = (notification) =>
       {
          ClaimsIdentity identity = 
               notification.AuthenticationTicket.Identity;
           var emailid = identity.Name;
           var res = new LogOnModel().GetAuthenticatedUserDetails(emailid);

           if (res != null && res.UserId > 0)
           {
             var claims = new UserModulePermissions().AddUserClaims(res);
             identity.AddClaims(claims);
           }
           else
          {
             notification.AuthenticationTicket.Properties.RedirectUri = 
             "/unauthorized";
             return Task.FromResult(0);
           }

           return Task.FromResult(notification);
         }
     }
  });

}

帐户管理员:

  public void SignIn()
 {

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

注意:

表单登录和广告登录都应该有效。我们可以使用表格登录或azure登录登录。

0 个答案:

没有答案