مصادقة النماذج - عرض تسجيل حاليا في معلومات المستخدم في FormView باستخدام مصدر بيانات كائن

StackOverflow https://stackoverflow.com/questions/1619938

سؤال

في تطبيق الويب الخاص بي يمكنني استخدام مصادقة النماذج مع ملف تعريف الارتباط. في صفحة وأتمنى لعرض معلومات عن دخوله حاليا في المستخدم، داخل FormView مدعوم من ObjectDataSource. مصدر البيانات الخاصة بي لديه حدد طريقة قبول كمعلمة اسم المستخدم الذي سيتم طلب بيانات المستخدم من قاعدة البيانات. كيف يمكنني الحصول على اسم المستخدم دخوله حاليا في المستخدم واستخدامها كمعلمة مختارة لمصدر البيانات.

هل كانت مفيدة؟

المحلول 2

وبدلا من ذلك، لقد اخترت استخدام هذا الحدث يؤدي تحديد من مصدر البيانات وإضافة المعلومات اللازمة باعتبارها inputParameter.

نصائح أخرى

في Global.asax .. يجب أن تكتب:

protected void Application_AuthenticateRequest(object sender, EventArgs e) {
    if (Request.PhysicalPath.EndsWith(".aspx") || Request.PhysicalPath.EndsWith(".axd"))
        SecurityManager.SetPrincipal();
}

وطريقة SecurityManager.SetPrincipal () يجب أن تبدو:

// variable we'll use to set HttpContext.Current.User
        IPrincipal principal = null;
        FormsIdentity identity;

        //IsAuthenticated will be automatically set by .NET framework
        if (HttpContext.Current.Request.IsAuthenticated)
        {
            // (FormsIdentity)HttpContext.Current.User.Identity will
            // be filled automatically by the .NET framework when using forms authentication
            identity = (FormsIdentity)HttpContext.Current.User.Identity;

            // This User class must be defined BY YOU
            User userProfile;
            // this user data is the data that you entered when you created the ticket.
            // this should be a security token that would allow you to GET THE USER FROM IT
            String userData = (((FormsIdentity)identity).Ticket).UserData;
            try
            {
                // UserHelper is a class that must be able to OBTAIN a USER given a SECURITY TOKEN.
                // remember, you created this token when you created the ticket you used in the cookie.
                userProfile = UserHelper.GetUser(userData);

                // AuthenticatedPrincipal must implement IPrincipal. Consider deriving from GenericPrincipal.
                // Your IPrincipal implementations must hold a reference to the UserClass you created
                principal = new AuthenticatedPrincipal(identity, userProfile);
            }
            catch
            {
                FormsAuthentication.SignOut();
                // This is analogous to AuthenticatedPrincipal
                principal = new AnonymousPrincipal(new GuestIdentity(), UserHelper.GetUser(null));
            }

        }
        else
        {
            principal = new AnonymousPrincipal(new GuestIdentity(), UserHelper.GetUser(null));
        }

        // Now we make our principal, that holds a reference to the currently
        // logged user, globally visible
        HttpContext.Current.User = principal;

وبقدر ما أعرف، ObjectDataSource يسمح لك لكتابة فئة طبقة الوصول إلى البيانات وتعيين بعض الأساليب من هذه الفئة لعمليات مصدر البيانات. يمكنك الوصول إلى HttpContext.Current.User من داخل أساليب الأطروحات.

وكما قلت أنت "في تطبيق الويب الخاص بي يمكنني استخدام مصادقة النماذج مع كوكي". أفترض أنك تعرف كيفية "تسجيل" للمستخدم وإرسال ملف تعريف الارتباط المستعرض. إذا كان لديك أي مشاكل مع هذا، اسمحوا لي أن أعرف.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top