<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://geeks.ms/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Marc Rubiño : asp.net, utilidades</title><link>http://geeks.ms/blogs/mrubino/archive/tags/asp.net/utilidades/default.aspx</link><description>Etiquetas: asp.net, utilidades</description><dc:language /><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Perfiles de Usuarios ASP.NET &amp; MVC</title><link>http://geeks.ms/blogs/mrubino/archive/2011/02/13/perfiles-de-usuarios-asp-net-amp-mvc.aspx</link><pubDate>Sun, 13 Feb 2011 17:05:00 GMT</pubDate><guid isPermaLink="false">2a2e7ade-7474-448b-9de5-1515d8bb7d1b:188903</guid><dc:creator>Marc Rubiño</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.ms/blogs/mrubino/rsscomments.aspx?PostID=188903</wfw:commentRss><comments>http://geeks.ms/blogs/mrubino/archive/2011/02/13/perfiles-de-usuarios-asp-net-amp-mvc.aspx#comments</comments><description>&lt;p align="justify"&gt;Una de las dudas m&amp;aacute;s habituales en los foros de ASP.NET, es como mantener una variable disponible para un usuario. Lo primero que se nos puede pasar por la cabeza es utilizar la sesi&amp;oacute;n para guardar esta informaci&amp;oacute;n, pero desde ASP.NET 2.0 tenemos otras alternativas m&amp;aacute;s adecuadas para estas cosas y adem&amp;aacute;s nos puede servir tanto para usuarios an&amp;oacute;nimos, como para usuarios autentificados.&lt;/p&gt;
&lt;p align="center"&gt;&lt;span style="background-color:#d19049;font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;span style="font-size:small;"&gt;&lt;strong&gt;&amp;nbsp; &amp;iexcl;&amp;iexcl; No tenemos que utilizar la sesi&amp;oacute;n para todo !!!&amp;nbsp; y tanto&lt;/strong&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="font-size:small;"&gt;&lt;strong&gt; nos sirve para ASP.NET como para ASP.NET MVC&lt;/strong&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="background-color:#d19049;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4 align="justify"&gt;Usuarios autentificados.&lt;/h4&gt;
&lt;p align="justify"&gt;Una vez el usuario se ha autentificado, se asocia el perfil a la identidad del usuario en el contexto actual &lt;a target="_blank" href="http://msdn.microsoft.com/es-es/library/system.web.httpcontext.user(v=vs.80).aspx"&gt;system.web.httpcontext.user&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;h4 align="justify"&gt;Usuarios an&amp;oacute;nimos&lt;/h4&gt;
&lt;p align="justify"&gt;El perfil para los usuarios an&amp;oacute;nimos no est&amp;aacute; habilitada de forma predeterminada y hay que especificarlo de forma expl&amp;iacute;cita.&lt;/p&gt;
&lt;p align="justify"&gt;Para poder tener estas propiedades del perfil habilitadas para los usuarios an&amp;oacute;nimos ASP.NET crea una cookie donde se establece una identidad &amp;uacute;nica para el usuario y poderlo identificar cada vez que el usuario acceda a la p&amp;aacute;gina web. Esta cookie tiene una caducidad de 70 d&amp;iacute;as y se renueva autom&amp;aacute;ticamente cuando el usuario accede al sitio.&lt;/p&gt;
&lt;p align="justify"&gt;Si necesitamos controlar cuando un usuario pasa de ser an&amp;oacute;nimo a usuario autentificado, podemos controlar este paso en el momento que el usuario se autentifica y se lanza el evento &lt;a href="http://msdn.microsoft.com/es-es/library/system.web.profile.profilemodule.migrateanonymous(v=vs.80).aspx"&gt;MigrateAnonymous&lt;/a&gt; incluido en el fichero Global.asax.&lt;/p&gt;
&lt;h4 align="justify"&gt;Tipos Soportados&lt;/h4&gt;
&lt;p align="justify"&gt;Si no se especifica el tipo del dato que queremos mantener, por defecto esta propiedad se guardara como string, pero podemos guardar tipos escalares (texto, n&amp;uacute;meros, fechas) tan solo especificando el tipo en la configuraci&amp;oacute;n de la propiedad.&lt;/p&gt;
&lt;p align="center"&gt;&lt;a href="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/image_5F00_4EE09945.png"&gt;&lt;img height="49" width="514" src="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/image_5F00_thumb_5F00_3268C8C7.png" alt="image" border="0" title="image" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Para trabajar con tipos complejos o propios tenemos que especificar el tipo de serializaci&amp;oacute;n que queremos utilizar.&lt;/p&gt;
&lt;p&gt;Nos tenemos que acordar de marcar nuestras clases como serializables para no tener problemas al guardar los datos.&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;namespace Demo.Profile
{
    [System.Serializable]
    public class Persona
    {
        public string Nombre { get; set; }
        public int Edad { get; set; }
        public DateTime Cumple { get; set; }
    }
}&lt;/pre&gt;
&lt;p&gt;Y cuando especificamos el tipo en el fichero de configuraci&amp;oacute;n no nos tenemos que olvidar el namespace.&lt;/p&gt;
&lt;p align="center"&gt;&lt;a href="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/image_5F00_75D35ADA.png"&gt;&lt;img height="63" width="520" src="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/image_5F00_thumb_5F00_1A677910.png" alt="image" border="0" title="image" style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;Proveedores&lt;/h4&gt;
&lt;p&gt;Otra de las ventajas para utilizar los perfiles de usuario es que se basan en los famosos proveedores de ASP.NET y podemos definir donde guardar esta informaci&amp;oacute;n, en base de datos, un fichero xml, etc.&lt;/p&gt;
&lt;p align="center"&gt;&lt;a href="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/1000_5F00_18_5F00_249_5F00_Aa478948_5F00_asp2prvdr0101en_2D00_usMSDN_5F00_10_5F00_65E86F25.gif"&gt;&lt;img height="328" width="513" src="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/1000_5F00_18_5F00_249_5F00_Aa478948_5F00_asp2prvdr0101en_2D00_usMSDN_5F00_10_5F00_thumb_5F00_46C7E2F6.gif" alt="1000_18_249_Aa478948_asp2prvdr0101en-usMSDN_10" border="0" title="1000_18_249_Aa478948_asp2prvdr0101en-usMSDN_10" style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Para este ejemplo he utilizado la tabla de sqlExpres que viene por defecto con nuestras aplicaciones ASP.NET.&lt;/p&gt;
&lt;p align="center"&gt;&lt;a href="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/TablasProfile_5F00_6366EF7A.png"&gt;&lt;img height="408" width="347" src="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/TablasProfile_5F00_thumb_5F00_10EA9FEF.png" alt="TablasProfile" border="0" title="TablasProfile" style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;Utilizar los Perfiles de Usuario&lt;/h4&gt;
&lt;p align="justify"&gt;Una vez que tenemos todo configurado ya podemos comenzar a utilizar los perfiles seg&amp;uacute;n nuestras necesidades.&lt;/p&gt;
&lt;p align="justify"&gt;Lo primero que podemos necesitar es actuar dependiendo si el usuario actual es un usuario an&amp;oacute;nimo o es un usuario autentificado &amp;ldquo;&lt;strong&gt;Profile.IsAnonymous&lt;/strong&gt;&amp;rdquo; y establecer el valor de las variables que tenemos configurado en el perfil, finalmente nos tenemos que acordar de guardar la modificaciones y listos.&lt;/p&gt;
&lt;p align="justify"&gt;Guardamos tipos escalares:&lt;/p&gt;
&lt;p align="center"&gt;&lt;a href="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/GuradarProfile_5F00_1FB76678.png"&gt;&lt;img height="149" width="484" src="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/GuradarProfile_5F00_thumb_5F00_081FB905.png" alt="GuradarProfile" border="0" title="GuradarProfile" style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Guardamos tipos complejos:&lt;/p&gt;
&lt;p align="center"&gt;&lt;a href="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/PersonaProfile_5F00_49BC8602.png"&gt;&lt;img height="140" width="501" src="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/PersonaProfile_5F00_thumb_5F00_0B56C24F.png" alt="PersonaProfile" border="0" title="PersonaProfile" style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Luego dispondremos de esta informaci&amp;oacute;n para toda la aplicaci&amp;oacute;n y para este usuario.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/RecuperarProfile_5F00_5F3C560E.png"&gt;&lt;img height="84" width="539" src="http://geeks.ms/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/mrubino/RecuperarProfile_5F00_thumb_5F00_7CB137CB.png" alt="RecuperarProfile" border="0" title="RecuperarProfile" style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:block;float:none;border-top-width:0px;border-bottom-width:0px;margin-left:auto;border-left-width:0px;margin-right:auto;padding-top:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Con esto lo tenemos todo de una manera f&amp;aacute;cil y persistente. Recordar que NO todas las aplicaciones ASP.NET viven de las sesiones y desde ASP.NET2.0 disponemos de esta alternativa que podemos utilizar incluso con las aplicaciones MVC.&lt;/p&gt;
&lt;p&gt;CrossPosting: &lt;a href="http://mrubino.net/" title="http://mrubino.net/"&gt;http://mrubino.net/&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://geeks.ms/aggbug.aspx?PostID=188903" width="1" height="1"&gt;</description><category domain="http://geeks.ms/blogs/mrubino/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://geeks.ms/blogs/mrubino/archive/tags/utilidades/default.aspx">utilidades</category><category domain="http://geeks.ms/blogs/mrubino/archive/tags/MVC/default.aspx">MVC</category></item></channel></rss>