c# - Sign in as different user in ASP.MVC is not changing user identity -
i'm developing mvc web application using windows authentication. aim allow automated logging when page opened allow signing different user on demand. i'm trying utilize code here 'login user' mvc 4 windows authentication , here http://www.roelvanlisdonk.nl/?p=825 none of them working me.
i've simplified case maximum, looks follows:
public string logout() { authenticationattempts = authenticationattempts + 1; if (authenticationattempts == 1) { this.send401(); } var domain = user.identity.name.split('\\')[0]; var user = user.identity.name.split('\\')[1]; homecoming string.format("domain: {0}<br>user: {1}", domain, user); } /// <summary> /// send 401 response /// </summary> public void send401() { // create 401 response, browser show log-in dialogbox, asking user supply new credentials, // if browser not set "automaticaly sign in current credentials" response.buffer = true; response.statuscode = 401; response.statusdescription = "unauthorized"; // authentication header must supplied. header can changed negotiate when using keberos authentication response.addheader("www-authenticate", "ntlm"); // send 401 response response.end(); } private int _authenticationattempts = 0; public int authenticationattempts { { if (!string.isnullorempty(string.format("{0}", session["authenticationattempts"]))) { int.tryparse(session["authenticationattempts"].tostring(), out _authenticationattempts); } homecoming _authenticationattempts; } set { _authenticationattempts = value; session["authenticationattempts"] = _authenticationattempts; } }
when phone call logout action method first time i'm getting sign in window, when click okay user.identity still was.
edit:
i found that
request.servervariables["logon_user"]
stores newly logged user identity, why user.identity isn't changing?
step 1:open web.config file , create next modifications:
<!— <authentication mode="forms"> <forms loginurl="~/account/login" timeout="2880" /> </authentication> --> <authentication mode="windows" />
step 2:by default mvc apps uses form authentication , simple membership, need create ‘false’ in order run windows authentication.
<appsettings> <add key="webpages:version" value="2.0.0.0" /> <add key="webpages:enabled" value="false" /> <add key="preserveloginurl" value="true" /> <add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" /> <add key="autoformsauthentication" value="false" /> <add key="enablesimplemembership" value="false"/> </appsettings>
step 3:select project name in solution explorer , in property explorer, click enable windows authentication.
step 4:in property explorer can disable anonymous authentication if want finish website authenticated users on development server.
reference
c# asp.net asp.net-mvc asp.net-mvc-4 windows-authentication
No comments:
Post a Comment