2017-06-09 48 views
0

我正在使用owin openid connect身份驗證提供程序託管在一個單獨的域身份驗證。認證過程很好地工作。我能夠在身份服務器成功登錄後查看受限制的頁面。OWIN openid連接外部登錄不執行指定的回調url

但我希望外部身份服務器返回到「account/SignInCallback」控制器操作,以便我可以執行與成員帳戶相關的幾行代碼。在瀏覽器的網絡活動中,它顯示「302 Found」爲「account/SignInCallback」,但它不會觸及連接到它的斷點。它直接進入請求啓動網址,例如「帳號/儀表板」。

有沒有一種方法可以強制系統在登錄後返回特定的url,即使請求url是不同的?

public class AccountController : BaseController 
{ 
    public AccountController() : base() 
    { 
    } 

    [Authorize] 
    public ActionResult Dashboard() 
    { 
     return View(); 
    } 

    [HttpPost] 
    [AllowAnonymous] 
    public ActionResult SignInCallback() 
    { 
     if (User.Identity.IsAuthenticated) 
     { 
      // Read claims and execute member specific codes 
     } 
     return View(); 
    } 

    [AllowAnonymous] 
    public ActionResult Unauthorized() 
    { 
     return View(); 
    } 
} 

啓動類如下:

public sealed class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     string ClientCallbackUri = @"https://client.local/account/SignInCallback"; 
     string IdServBaseUri = @"https://idm.website.com/core"; 
     string TokenEndpoint = @"https://idm.website.com/core/connect/token"; 
     string UserInfoEndpoint = @"https://idm.website.com/core/connect/userinfo"; 
     string ClientId = @"WebPortalDemo"; 
     string ClientSecret = @"aG90apW2+DbX1wVnwwLD+eu17g3vPRIg7p1OnzT14TE="; 

     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "Cookies" 
     }); 

     app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
     { 
      ClientId = ClientId, 
      Authority = IdServBaseUri, 
      RedirectUri = ClientCallbackUri, 
      PostLogoutRedirectUri = ClientUri, 
      ResponseType = "code id_token token", 
      Scope = "openid profile roles", 
      TokenValidationParameters = new TokenValidationParameters 
      { 
       NameClaimType = "name", 
       RoleClaimType = "role" 
      }, 
      SignInAsAuthenticationType = "Cookies", 

      Notifications = new OpenIdConnectAuthenticationNotifications 
      { 
       AuthorizationCodeReceived = async n => 
       { 
        // use the code to get the access and refresh token 
        var tokenClient = new TokenClient(
         TokenEndpoint, 
         ClientId, 
         ClientSecret); 

        var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri); 

        if (tokenResponse.IsError) 
        { 
         throw new Exception(tokenResponse.Error); 
        } 

        // use the access token to retrieve claims from userinfo 
        var userInfoClient = new UserInfoClient(UserInfoEndpoint); 

        var userInfoResponse = await userInfoClient.GetAsync(tokenResponse.AccessToken); 

        // create new identity 
        var id = new ClaimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType); 
        //id.AddClaims(userInfoResponse.GetClaimsIdentity().Claims); 
        id.AddClaims(userInfoResponse.Claims); 

        id.AddClaim(new Claim("access_token", tokenResponse.AccessToken)); 
        id.AddClaim(new Claim("expires_at", DateTime.Now.AddSeconds(tokenResponse.ExpiresIn).ToLocalTime().ToString())); 
        id.AddClaim(new Claim("refresh_token", tokenResponse.RefreshToken)); 
        id.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken)); 
        id.AddClaim(new Claim("sid", n.AuthenticationTicket.Identity.FindFirst("sid").Value)); 

        n.AuthenticationTicket = new AuthenticationTicket(
         new ClaimsIdentity(id.Claims, n.AuthenticationTicket.Identity.AuthenticationType, "name", "role"), 
         n.AuthenticationTicket.Properties); 
       } 
      } 
     }); 
    } 
} 

回答

0

它看起來你需要的是設置

n.AuthenticationTicket.Properties.RedirectUri = n.RedirectUri; 

AuthorizationCodeReceived委託

0

個人身份驗證模板通過啓用AutomaticChallenge上的Cookie中間件而不是其它驗證的中間件(OIDC在這種情況下)做到這一點。 Cookie將其重定向到一個AccountController登錄頁面,然後他們選擇auth方法,執行auth重定向,返回到帳戶控制器以進行您想要添加的額外步驟,然後通過重定向回原始頁面完成。

這裏是一個模板ASP.NET核心的更高版本: https://github.com/aspnet/Templates/blob/rel/1.0.5/src/Rules/StarterWeb/IndividualAuth/Controllers/AccountController.cs https://github.com/aspnet/Templates/blob/rel/1.0.5/src/Rules/StarterWeb/IndividualAuth/Startup.cs

注意這在很大程度上是由身份管理的框架,但它不是必需的。