2014-12-04 256 views
3

我必須找到問題的解決方案。所以我正在開發網站,我堅持認證。首先,我們使用Azure Active Directory進行用戶存儲。所以我找到了WebApp-WebAPI-OpenIDConnect-DotNet,並使它適合我的需求。到目前爲止它工作正常。但現在我還必須實施外部登錄(facebook,twitter等)。因此,我評論了以前的所有工作,以便我處理此任務。我不得不重寫一些UserManager和UserStore類,但它已經可以工作了。我可以登錄Facebook。但是現在,當我需要將這兩個登錄名一起加入時,它們不起作用。看起來,他們在框架內發生衝突。 Facebook登錄需要app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);,但當我打開此功能時,天藍色登錄將停止工作。如果我評論這一點,天藍色的登錄工程,Facebook沒有。任何人都可以給我一些幫助解決這個問題嗎?我會提供我Startup.Auth.csAzure身份驗證和OAuth身份驗證

using System; 
using Owin; 
using Microsoft.Owin.Security; 
using Microsoft.Owin.Security.Cookies; 
using Microsoft.Owin.Security.OpenIdConnect; 
using System.Configuration; 
using System.Globalization; 
using Microsoft.AspNet.Identity; 
using Microsoft.Owin; 
using Microsoft.AspNet.Identity.Owin; 
using ClearRoadmapWeb.LoginProviderHelpers; 
using Microsoft.Owin.Security.Facebook; 
using System.Collections.Generic; 

namespace ClearRoadmapWeb 
{ 
    public partial class Startup 
    { 
     private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; 
     private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; 
     private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"]; 
     private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"]; 

     string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant); 

     public void ConfigureAuth(IAppBuilder app) 
     { 
      app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie); 
      app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

      app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

      app.UseOpenIdConnectAuthentication(
       new OpenIdConnectAuthenticationOptions 
       { 
        ClientId = clientId, 
        Authority = authority, 
        PostLogoutRedirectUri = postLogoutRedirectUri 
       } 
      ); 

      app.CreatePerOwinContext<AzureIdentityUserManager>(AzureIdentityUserManager.Create); //For Faceook 
      app.CreatePerOwinContext<AzureIdentitySignInManager>(AzureIdentitySignInManager.Create); //For Facebook 

      #region FacebookOptions 
      //app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

      FacebookAuthenticationOptions facebookOptions = new FacebookAuthenticationOptions() 
      { 
       AppId = "fb appId", 
       AppSecret = "fb appSecret" 
      }; 
      facebookOptions.Scope.Add("email"); 
      facebookOptions.Provider = new FacebookAuthenticationProvider() 
      { 
       OnAuthenticated = async context => 
       { 
        foreach (var x in context.User) 
        { 
         context.Identity.AddClaim(new System.Security.Claims.Claim(x.Key, x.Value.ToString())); 
        } 
        //Get the access token from FB and store it in the database and use FacebookC# SDK to get more information about the user 
        context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken)); 
       } 
      }; 

      #endregion 
      app.UseFacebookAuthentication(facebookOptions); 
     } 
    } 
} 

回答

0

默認情況下OpenIdConnect認證模式是有效的。這意味着oidc將總是嘗試處理授權。對我來說,適用於在控制器方法中發出直接挑戰,如下所示:

HttpContext.GetOwinContext()。Authentication.Challenge(「FaceBook」);

這是編碼Startup_Auth如後:

public void Configure(IAppBuilder app) 
    { 
     CookieAuthenticationExtensions.UseCookieAuthentication(
      app, 
      new CookieAuthenticationOptions 
      { 
       AuthenticationType = "FaceBook", 
      }); 

     FacebookAuthenticationExtensions.UseFacebookAuthentication(
      app, 
      new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions 
      { 
       AppId = "...", 
       AppSecret = "...", 
       AuthenticationType = "FaceBook", 
       SignInAsAuthenticationType = "FaceBook", 
      }); 

     CookieAuthenticationExtensions.UseCookieAuthentication(
     app, 
     new CookieAuthenticationOptions 
     { 
      AuthenticationType = "OpenIdConnect", 
     }); 

     OpenIdConnectAuthenticationExtensions.UseOpenIdConnectAuthentication(
      app, 
      new Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions 
      { 
       AuthenticationType = "OpenIdConnect", 
       AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive, 
       ClientId = "...", 
       Authority = "...", 
       SignInAsAuthenticationType = "OpenIdConnect" 
      }); 

你需要確保當你想AAD和FB之間「開關」的身份,你清除註銷現有的身份或清除當前餅乾。

+0

OpenID的工作,但對於臉譜,我不得不改變''FaceBook「'到'」Facebook「'。但後來,在Facebook登錄後,它將我循環到'https:// localhost:44300/Account/ExternalLogin?provider = Facebook&error = access_denied&error = access_denied ...&error = access_denied ...'(每次添加'&error = access_denied' ) – Wish 2014-12-08 08:11:00