Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 2.0 Security, Membership, and Role Management ISBN: 978-0-7645-9698-8
This is the forum to discuss the Wrox book Professional ASP.NET 2.0 Security, Membership, and Role Management by Stefan Schackow; ISBN: 9780764596988
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 2.0 Security, Membership, and Role Management ISBN: 978-0-7645-9698-8 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 12th, 2006, 11:19 AM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default manage users accounts

hello, i'm using the default db with visual studio 2005 the aspnetdb but ther's a bug in visual studio when i use sqldatasource to the aspnt_users table i only find the view of the table not the base table, so i dont know what i shoud do to manage my accounts i only want to delete accounts nothing else.thanks

Reply With Quote
  #2 (permalink)  
Old April 12th, 2006, 12:00 PM
Wrox Author
 
Join Date: Feb 2006
Location: Redmond, Washington, USA.
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It turns out the data source folks explicitly screened out all of the built-in tables from their wizard. The theory being that this would reduce clutter when using data sources.

Try initially configuring the data source to work against the view of the aspnet_users table. Then switch to source view on your page and change the SQL to instead reference the real table.

-Stefan
Reply With Quote
  #3 (permalink)  
Old April 13th, 2006, 02:33 AM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello stefsch, thanks for ur helpful reply i did what u said and it worked but i still have the delete problem i cant choose it from the datasource cos i have only the view of the table should i add it also in the source view, and another something in the admin tool when u manage users delete user for example the user deleted from the aspnet users and the aspnet membership so i think i will have a problem when i will try to delete the user from the aspnet users!! what do u think? , waiting for ur reply. thanks

Reply With Quote
  #4 (permalink)  
Old April 13th, 2006, 12:15 PM
Wrox Author
 
Join Date: Feb 2006
Location: Redmond, Washington, USA.
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can do the same trick - set everything up against the view, and then in source switch it over to the real table name.

You are correct that deleting a user this way through the datasource is a problem. Fundamentally Membership is not a directly data-bindable feature. Usually what we recommend is:

1. Use ObjectDataSource - *not* SqlDataSource.

2. In App_Code, write a really simple class with static (in VB Shared) Select, Insert, Update, Delete methods that internally call into Membership. The reason for this is that the Membership type itself does not have all the necessary method signatures expected by ObjectDataSource to show up in the the data source wizard. As a result, you need a type in App_Code so the wizard can find it. Although this approach is more work - it does allow you to route update, insert and delete operations through the Membership provider. Which automatically takes care of things like deleting rows from tables in the correct order.

-Stefan
Reply With Quote
  #5 (permalink)  
Old April 13th, 2006, 03:21 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello Stefan, thanks for ur reply but i didnt know how to do what u said i'm kind a beginner, i working in admin page the users page which i can manage my accounts i dont know if i should delete the account or lock it but to do that i will use the membership table not the aspnet users table, one last thing could u plzzz write an example code a small one just to make it clear to me. thanks for the help

Reply With Quote
  #6 (permalink)  
Old April 13th, 2006, 09:05 PM
Wrox Author
 
Join Date: Feb 2006
Location: Redmond, Washington, USA.
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok - this is C# code showing how to see a list of users, and then click on one of the rows in a GridView to delete a user.

First, in App_Code, create a class with the following code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public static class MembershipWrapper
{
    public static MembershipUser GetUser(string username)
    {
        return Membership.GetUser(username);
    }

    public static bool DeleteUser(string username)
    {
        return Membership.DeleteUser(username);
    }

    public static MembershipUserCollection GetAllUsers()
    {
        return Membership.GetAllUsers();
    }
}


Then create a web page, and in the .aspx place the following markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView DataKeyNames="UserName" ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="ProviderName" HeaderText="ProviderName" ReadOnly="True"
                    SortExpression="ProviderName" />
                <asp:CheckBoxField DataField="IsOnline" HeaderText="IsOnline" ReadOnly="True" SortExpression="IsOnline" />
                <asp:BoundField DataField="LastPasswordChangedDate" HeaderText="LastPasswordChangedDate"
                    ReadOnly="True" SortExpression="LastPasswordChangedDate" />
                <asp:BoundField DataField="PasswordQuestion" HeaderText="PasswordQuestion" ReadOnly="True"
                    SortExpression="PasswordQuestion" />
                <asp:CheckBoxField DataField="IsLockedOut" HeaderText="IsLockedOut" ReadOnly="True"
                    SortExpression="IsLockedOut" />
                <asp:BoundField DataField="Comment" HeaderText="Comment" SortExpression="Comment" />
                <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:BoundField DataField="CreationDate" HeaderText="CreationDate" ReadOnly="True"
                    SortExpression="CreationDate" />
                <asp:CheckBoxField DataField="IsApproved" HeaderText="IsApproved" SortExpression="IsApproved" />
                <asp:BoundField DataField="LastLockoutDate" HeaderText="LastLockoutDate" ReadOnly="True"
                    SortExpression="LastLockoutDate" />
                <asp:BoundField DataField="LastLoginDate" HeaderText="LastLoginDate" SortExpression="LastLoginDate" />
                <asp:BoundField DataField="LastActivityDate" HeaderText="LastActivityDate" SortExpression="LastActivityDate" />
            </Columns>
        </asp:GridView>

    </div>
        <br />
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="DeleteUser"
            SelectMethod="GetAllUsers" TypeName="MembershipWrapper">
            <DeleteParameters>
                <asp:Parameter Name="username" Type="String" />
            </DeleteParameters>
        </asp:ObjectDataSource>
    </form>
</body>
</html>

Now when you run this, the ObjectDataSource binds to the wrapper object to fetch all the users and display them in a Gridview. There is also a delete link rendered for each row.

If you click the delete link, the GridView passes the UserName value to the delete method and the user is deleted from the system.

-Stefan
Reply With Quote
  #7 (permalink)  
Old April 14th, 2006, 06:04 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello Stefan, it worked(yeeessss:)) u are a real genius, now i can delete users from the table, there's some columns i dont need it to appear in the gridview i will do that from the edit columns no problem, just one little thing u add the user name field from the aspnet users and the datasource deals with aspnet membership what i should do to add another columns from the aspnet users? i tried to put a tag like that <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
and changed the "username" with the column i want but it didnt work it says that "A field or property with the name 'name' was not found on the selected data source" ?!! waiting for ur reply. thanks for all that help


Reply With Quote
  #8 (permalink)  
Old April 14th, 2006, 08:25 PM
Wrox Author
 
Join Date: Feb 2006
Location: Redmond, Washington, USA.
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Since the grid is binding to an ObjectDataSource, it is actually rendering the properties on MembershipUser - *not* columns from a table like aspnet_Users.

The example below has all of the properties from MembershipUser - so there isn't anything more that can be added. The only important data from aspnet_Users are really the UserName and LastActivityDate columns. Those are available as properties on MembershipUser. The other columns in aspnet_Users are either not used by ASP.NET (i.e. MobileAlias) or not relevant (i.e. IsAnonymous).

The reason why I bound ObjectDataSource to MembershipUser is that the Membership feature is usually what you want to use for managing accounts.

-Stefan
Reply With Quote
  #9 (permalink)  
Old April 15th, 2006, 06:28 AM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello Stefan, ok i just have some other columns i add manually to the aspnet users so this why i want to show these columns, anyway thanks for helping me in this problem it's much much better, thanks.

Reply With Quote
  #10 (permalink)  
Old April 28th, 2006, 02:41 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Amazing example... it really help me a lot!.

Hey, but how could i modify de class to update users too?.
In advance, thanks a lot! :)



Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Users with accounts PankajGarg10 General .NET 0 April 13th, 2007 01:06 AM
Accounts module - adding new users andieje BOOK: ASP.NET Website Programming Problem-Design-Solution 1 June 26th, 2006 11:07 PM
Accounts RM82 BOOK: ASP.NET Website Programming Problem-Design-Solution 2 April 7th, 2004 09:37 PM
Enums for accounts??? rcarter BOOK: ASP.NET Website Programming Problem-Design-Solution 3 November 7th, 2003 11:07 PM



All times are GMT -4. The time now is 03:00 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.