RedirectFromLoginPage() is not updating User.Ident

2019-09-06 11:25发布

问题:

I use FormsAuthentication.RedirectFromLoginPage(userName.Trim(), false); to set the User.Identity.Name field that I reference later. When I execute this line, the User.Identity object does not update at all; it contains whatever it was previously set to. All the documentation I see online says this should update my User.Identity object with the correct name, but I don't see that happening.

I have the web config set up properly with the following lines:

<authentication mode="Forms">
    <forms name="formsauth" loginUrl="Login.aspx" protection="All" timeout="60">
    </forms>
</authentication>
<authorization>
    <deny users="?"/>
</authorization>

I am relatively new to this stuff, so any help is appreciated. Thanks!

回答1:

It will be updated on the next request but not on the next line following it. RedirectFromLoginPage sets an authentication cookie in the client browser that will be read upon the next client request and you will see the User.Identity.Name property updated. Updating this property in the same HTTP request is meaningless as you already know that the user passed authentication as you called the method.