How to pass username after login redirect?

2019-07-14 06:00发布

问题:

I have 2 applications: one does the login (app1) and another one does other stuff that I want (app2). The app1 is built in ASP.NET Boilerplate and what I want to do is after login with this app, I want to redirect to the app2. Until now everything is fine, I just used the returnUrl parameter to redirect to the app2. The problem is that I want to pass to app2 some of the login information like username. It's not supposed to change anything in app1, so how can I do that?

Both app1 and app2 are built in MVC 5.

回答1:

In app1, the user is available from loginResult.User as you can see in this line:

await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe);

You can append the username as a query parameter after checking returnUrl:

if (string.IsNullOrWhiteSpace(returnUrl))
{
    returnUrl = Request.ApplicationPath;
}
else
{
    returnUrl += "?username=" + loginResult.User.UserName; // This line
}

And retrieve that in app2.