Autenticazione moduli: visualizza le informazioni dell'utente attualmente connesso in FormView utilizzando la sorgente dati oggetto

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

Domanda

Nella mia applicazione Web utilizzo l'autenticazione moduli con un cookie. In una pagina desidero visualizzare le informazioni per l'utente attualmente connesso, all'interno di un FormView alimentato da un ObjectDataSource. La mia origine dati ha un metodo select che accetta come parametro il nome utente con cui i dati dell'utente verranno richiesti dal database. Come posso ottenere il nome utente per l'utente attualmente connesso e utilizzarlo come parametro di selezione per l'origine dati.

È stato utile?

Soluzione 2

Invece, ho scelto di utilizzare l'evento Selecting dell'origine dati e aggiungere le informazioni di cui avevo bisogno come inputParameter.

Altri suggerimenti

Su Global.asax .. dovresti scrivere:

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

Il metodo SecurityManager.SetPrincipal () dovrebbe apparire come:

// 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;

Per quanto ne so, ObjectDataSource consente di scrivere una classe Layer di accesso ai dati e mappare alcuni metodi di questa classe alle operazioni DataSource. Puoi accedere a HttpContext.Current.User da questi metodi.

Come hai detto che sei " Nella mia applicazione web utilizzo l'autenticazione moduli con un cookie " ;. Suppongo che tu sappia come " accedere " l'utente e invia il cookie al browser. In caso di problemi, fatemelo sapere.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top