ASP.NET、Windows身份验证和querystring

本文关键字:querystring 身份验证 Windows NET ASP | 更新日期: 2023-09-27 18:14:06

场景:ASP。NET C#与sqlserver 2014在windows服务器2012上运行。在web.config中设置的通过Windows身份验证的用户身份验证如下:

<authentication mode="Windows" />
<authorization>
        <deny users="?" />
        <allow users="*" />
</authorization>   

该网站适用于在服务器中拥有本地帐户的用户。没有活动目录。同一域下没有。

当用户按照打开网站页面时

http://<mydomain>/default.aspx

需要Windows身份验证:出现一个Windows对话框。用户插入自己的用户名和密码。如果凭据有效,则对用户进行身份验证,并且页面

http://<mydomain>/default.aspx

已加载。当前会话不再需要身份验证(直到用户关闭浏览器或重新启动电脑(。

假设一个用户没有在网站上进行身份验证,他想按照打开url

http://<mydomain>/default.aspx?id=1

注意查询字符串

id=1

由页面default.aspx用于从数据库加载数据(例如(。

由于用户未通过身份验证,Windows对话框需要用户名和密码。用户插入正确的凭据,即可进行身份验证。页面

http://<mydomain>/default.aspx

已加载。

注意查询字符串

id=1

缺少

用户希望按照打开链接

http://<mydomain>/default.aspx?id=1

但是在验证后页面

http://<mydomain>/default.aspx

已加载。缺少查询字符串。因此,用户必须在浏览器中再次插入url

http://<mydomain>/default.aspx?id=1

web.config文件如下

<system.web>
    <customErrors mode="Off" />
    <pages validateRequest="false" controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID"/>
    <authentication mode="Windows" />
    <authorization>
            <deny users="?" />
            <allow users="*" />
    </authorization>   
    <membership>
    <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
    </providers>
    </membership>
    <profile>
        <providers>
            <clear />
            <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
        </providers>
    </profile>
    <roleManager enabled="false">
    <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
        </providers>
    </roleManager>
        <sessionState mode="InProc" timeout="120" />
</system.web>

是否可以在url中维护身份验证后的查询字符串?IIS中是否存在这样一种保护,即在身份验证之前不考虑查询字符串?

感谢

ASP.NET、Windows身份验证和querystring

在登录页面的代码隐藏中(或在您放置登录表单的任何位置(,尝试以下操作:

// Login Button Click Event
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //verify user's credential 
            // if it is correct then 
            // do whatever you want, create a session, cookie etc..., then redirect user:     
                        string strRedirect;
                        strRedirect = Request["ReturnUrl"];
                        if (strRedirect == null) strRedirect = "/"; // if there isn't any query string then redirect user to default page
                        Response.Redirect(strRedirect, true);
            // else display wrong username/password message...
        }