繁体   English   中英

ASP.NET、Windows 身份验证和查询字符串

[英]ASP.NET, Windows Authentication and querystring

场景:使用 sqlserver 2014 的 Windows Server 2012 上的 ASP.NET C# 网站。通过在 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 中是否有这样的保护,即在身份验证之前不考虑查询字符串?

谢谢

在登录页面(或放置登录表单的任何地方)后面的代码中,试试这个:

// 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...
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM