2016-08-25 1 views
2

나는이에 관한 질문을 찾는 시도를 프로파일 편집 이동하지만, 아무것도 찾을 수 없습니다.푸른 AD의 B2C - 없음은

나는 인증을 위해 푸른 광고의 B2C를 사용하는 ASP.NET 코어 1.0 응용 프로그램을 가지고있다. 서명 및 등록은 물론 로그 아웃도 잘합니다. 문제는 내가 사용자 프로필을 편집하려고 시도 할 때 발생합니다. 여기

namespace AspNetCoreBtoC 
{ 
    public class Startup 
    { 
     private IConfigurationRoot Configuration { get; } 

     public Startup(IHostingEnvironment env) 
     { 
      var builder = new ConfigurationBuilder() 
          .SetBasePath(env.ContentRootPath) 
          .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 
          .AddEnvironmentVariables(); 
      Configuration = builder.Build(); 
     } 

     // This method gets called by the runtime. Use this method to add services to the container. 
     // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 
     public void ConfigureServices(IServiceCollection services) 
     { 
      services.AddSingleton<IConfiguration>(Configuration); 
      services.AddMvc(); 
      services.AddAuthentication(
       opts => opts.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(); 

      if (env.IsDevelopment()) 
      { 
       loggerFactory.AddDebug(LogLevel.Debug); 
       app.UseDeveloperExceptionPage(); 
      } 

      app.UseStaticFiles(); 

      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AutomaticChallenge = false 
      }); 

      string signUpPolicyId = Configuration["AzureAd:SignUpPolicyId"]; 
      string signUpCallbackPath = Configuration["AzureAd:SignUpCallbackPath"]; 
      app.UseOpenIdConnectAuthentication(CreateOidConnectOptionsForPolicy(signUpPolicyId, false, signUpCallbackPath)); 

      string userProfilePolicyId = Configuration["AzureAd:UserProfilePolicyId"]; 
      string profileCallbackPath = Configuration["AzureAd:ProfileCallbackPath"]; 
      app.UseOpenIdConnectAuthentication(CreateOidConnectOptionsForPolicy(userProfilePolicyId, false, profileCallbackPath)); 

      string signInPolicyId = Configuration["AzureAd:SignInPolicyId"]; 
      string signInCallbackPath = Configuration["AzureAd:SignInCallbackPath"]; 
      app.UseOpenIdConnectAuthentication(CreateOidConnectOptionsForPolicy(signInPolicyId, true, signInCallbackPath)); 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "Default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 
      }); 
     } 

     private OpenIdConnectOptions CreateOidConnectOptionsForPolicy(string policyId, bool autoChallenge, string callbackPath) 
     { 
      string aadInstance = Configuration["AzureAd:AadInstance"]; 
      string tenant = Configuration["AzureAd:Tenant"]; 
      string clientId = Configuration["AzureAd:ClientId"]; 
      string redirectUri = Configuration["AzureAd:RedirectUri"]; 

      var opts = new OpenIdConnectOptions 
      { 
       AuthenticationScheme = policyId, 
       MetadataAddress = string.Format(aadInstance, tenant, policyId), 
       ClientId = clientId, 
       PostLogoutRedirectUri = redirectUri, 
       ResponseType = "id_token", 
       TokenValidationParameters = new TokenValidationParameters 
       { 
        NameClaimType = "name" 
       }, 
       CallbackPath = callbackPath, 
       AutomaticChallenge = autoChallenge 
      }; 

      opts.Scope.Add("openid"); 

      return opts; 
     } 
    } 
} 

내 AccountController은 내가 미들웨어에 도전 발행 곳에서, : 나는 OWIN 예에서 적응하기 위해 노력했습니다

namespace AspNetCoreBtoC.Controllers 
{ 
    public class AccountController : Controller 
    { 
     private readonly IConfiguration config; 

     public AccountController(IConfiguration config) 
     { 
      this.config = config; 
     } 

     public IActionResult SignIn() 
     { 
      return Challenge(new AuthenticationProperties 
      { 
       RedirectUri = "/" 
      }, 
      config["AzureAd:SignInPolicyId"]); 
     } 

     public IActionResult SignUp() 
     { 
      return Challenge(new AuthenticationProperties 
      { 
       RedirectUri = "/" 
      }, 
      config["AzureAd:SignUpPolicyId"]); 
     } 

     public IActionResult EditProfile() 
     { 
      return Challenge(new AuthenticationProperties 
      { 
       RedirectUri = "/" 
      }, 
      config["AzureAd:UserProfilePolicyId"]); 
     } 

     public IActionResult SignOut() 
     { 
      string returnUrl = Url.Action(
       action: nameof(SignedOut), 
       controller: "Account", 
       values: null, 
       protocol: Request.Scheme); 
      return SignOut(new AuthenticationProperties 
      { 
       RedirectUri = returnUrl 
      }, 
      config["AzureAd:UserProfilePolicyId"], 
      config["AzureAd:SignUpPolicyId"], 
      config["AzureAd:SignInPolicyId"], 
      CookieAuthenticationDefaults.AuthenticationScheme); 
     } 

     public IActionResult SignedOut() 
     { 
      return View(); 
     } 
    } 
} 

을 여기처럼 내 Startup.cs는 모습입니다 . 내가 가진 문제는 프로필을 편집하기 위해 OpenIdConnect 미들웨어에 대한 책임을 제기해야한다는 것입니다. 문제는 미들웨어 (쿠키)의 기본 로그인을 호출하여 사용자가 인증되었음을 알게되므로 액션이 무단으로 처리되어 있어야하며/Account/AccessDenied로 리디렉션을 시도합니다 (비록 내가 Azure AD로 이동하여 프로파일을 편집해야하는 대신 해당 경로에있는 항목이 있어야합니다.

누구든지 ASP.NET 코어에서 사용자 프로필 편집을 성공적으로 구현 했습니까?

답변

1

글쎄, 나는 마침내 그것을 해결했다. 나는 해결책을 포함하는 체제에 blog 기사를 썼다 : https://joonasw.net/view/azure-ad-b2c-with-aspnet-core. 문제는 ChallengeBehavior이며 기본값 인 Automatic 대신 Unauthorized로 설정해야합니다. 이 순간에 프레임 워크 ChallengeResult로 정의 할 수없는, 그래서 난 내 자신을 만들어 :

public class MyChallengeResult : IActionResult 
{ 
    private readonly AuthenticationProperties authenticationProperties; 
    private readonly string[] authenticationSchemes; 
    private readonly ChallengeBehavior challengeBehavior; 

    public MyChallengeResult(
     AuthenticationProperties authenticationProperties, 
     ChallengeBehavior challengeBehavior, 
     string[] authenticationSchemes) 
    { 
     this.authenticationProperties = authenticationProperties; 
     this.challengeBehavior = challengeBehavior; 
     this.authenticationSchemes = authenticationSchemes; 
    } 

    public async Task ExecuteResultAsync(ActionContext context) 
    { 
     AuthenticationManager authenticationManager = 
      context.HttpContext.Authentication; 

     foreach (string scheme in authenticationSchemes) 
     { 
      await authenticationManager.ChallengeAsync(
       scheme, 
       authenticationProperties, 
       challengeBehavior); 
     } 
    } 
} 

죄송합니다 이름에 대한 ...하지만이 하나 컨트롤러 액션에서 반환 및 지정하여 할 수있다 ChallengeBehavior.Unauthorized, 나는 그것이해야하는대로 모든 것을 작동 시켰습니다.