【问题标题】:Resetting the Forms Authentication in a web method在 Web 方法中重置表单身份验证
【发布时间】:2014-05-21 09:26:50
【问题描述】:

我正在使用 ASP.Net compact 版本 3.5 S.P.I
我在一个定期调用网络方法的手持扫描仪上运行一个应用程序。
我已在 Web 配置中将表单身份验证超时设置为 60

<authentication mode="Forms">
<forms name="Auth" loginUrl="service1.asmx" protection="All" timeout="60" path="/" slidingExpiration="true" />

</authentication>

<p>

超时倒计时,当应用中的身份验证过期时,用户将重新登录。但是,如果我调用 web Method ,则根本不会重置超时。如果登录超时,用户不会返回登录屏幕。
这是我调用的一种 Web 方法的示例 我不希望为每个 Web 方法添加代码来完成重置超时,但如果我必须这样做的话。

[WebService(命名空间 = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [工具箱项目(假)] 公共类 STAWMS_WebService :WebService { 私有数据表 myCommonTable = new DataTable("Common"); 私有数据表 myReturnData = new DataTable("ReturnData"); 私有数据集 myReturnDataSet = new DataSet(); private string SessionID = HttpContext.Current.Session.SessionID; private string query; private int UserPK; private ArrayList myparams = new ArrayList(); private DataSet myTempDataSet = new DataSet(); private int ReturnIntegerTable; private int ReturnBooleanTable; [WebMethod(EnableSession = true)] public DataSet GetBuildUnitDetails(int StockInPK, int UserInPK) { try { if (Context.User.Identity.IsAuthenticated) { myparams.Clear(); myparams.Add(new STASqlParameter("@StockInPK", StockInPK)); myReturnData = STADatabase.SQLStoredProcedureSelect("GetStockInfo", myparams).Tables[0]; myReturnData.TableName = "STA2"; myReturnDataSet.Tables.Add(myReturnData.Copy()); PopulateMyCommonTable(true, SessionID, true, UserInPK); } return myReturnDataSet; } catch (Exception) { PopulateMyCommonTable(false, SessionID, false, UserInPK); return myReturnDataSet; } } } </pre>

【问题讨论】:

    标签: c# asp.net web-services


    【解决方案1】:

    作为一种解决方法,您可以继续以下操作:

    如果您的 Context.User.Identity.IsAuthenticatedfalse,则将 myReturnDataSet 返回为 null。因此,您可以检查手持设备是否为空,只需重定向到登录页面即可。

        try
        {
            if (Context.User.Identity.IsAuthenticated)
            {
                ...
                return myReturnDataSet;
            }
            else
            {
                return null;//Here it will sign you is not authenticated.
            }            
        }
        catch (Exception)
        {
            ...
            return myReturnDataSet;
        }
    

    另外,我不知道在手持版本的框架中是如何工作的,但loginUrl="service1.asmx" 应该指向你的登录页面。

    【讨论】:

    • 你好。我将不得不在 .net 中编写一些代码来检查用户是否仍然登录,因为应用程序和 Web 服务之间有两个不同的会话 ID。
    猜你喜欢
    • 2015-06-26
    • 1970-01-01
    • 2015-11-03
    • 2013-10-30
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    相关资源
    最近更新 更多