How to redirect after Azure AD authentication to different controller action in ASP Net Core MVC

前端 未结 3 1903
孤街浪徒
孤街浪徒 2021-01-14 18:26

I have setup my ASP Net Core 2.0 project to authenticate with Azure AD (using the standard Azure AD Identity Authentication template in VS2017 which uses OIDC). Everything i

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 18:40

    I have found a way to make it work by using a redirect as follows...

    Inside Startup

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Account}/{action=SignIn}/{id?}");
    });
    

    Inside AccountController

    // GET: /Account/CheckSignIn
    [HttpGet]
    [Authorize]
    public IActionResult CheckSignIn()
    {
        //add code here to check if AzureAD identity exists in user table in local database
        //if not then insert new user record into local user table
    
        return RedirectToAction(nameof(HomeController.Index), "Home");
    }
    
    //
    // GET: /Account/SignIn
    [HttpGet]
    public IActionResult SignIn()
    {
        return Challenge(
            new AuthenticationProperties { RedirectUri = "/Account/CheckSignIn" }, OpenIdConnectDefaults.AuthenticationScheme);
    }
    

    Inside AzureAdServiceCollectionExtensions (.net core 2.0)

    private static Task RedirectToIdentityProvider(RedirectContext context)
    {
        if (context.Request.Path != new PathString("/"))
        {
            context.Properties.RedirectUri = new PathString("/Account/CheckSignIn");
        }
        return Task.FromResult(0);
    }
    

提交回复
热议问题