You're here: Snippet Directory » Microsoft .NET » ASP.NET (64)
Language:

Beispiel-Membership-Provider für ASP.NET 2.0

Language: Deutsch
Programming Language: C#
Published by: Thomas
Last Update: 5/8/2006
Views: 1494

Description

Zeigt die beispielhafte Implementierung eines MemberShoipProviders für ASP.NET 2.0 Dabei werden nur die wichtigsten Sachen erledigt, die nötig sind, um den Provider zu benutzen.

Allein schon damit lässt sich das Login-Control benutzen, mit dem man einen einfachen Login realisieren kann. Zugegriffen wird dabei auf eine "Kunden"-Tabelle in der Username ("KundeName") und Passwort ("Kundepasswort") gespeichert sind.

Der Zugriff erfolgt hier auf eine SQL-Server-Datenbank, kann aber auch locker auf Access und Co. umgeschrieben werden.

Code

1 using System; 2 using System.Data; 3 using System.Data.SqlClient; 4 using System.Configuration; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 12 using System.Collections.Specialized; 13 14 /// <summary> 15 /// Summary description for MyMembershipprovider 16 /// </summary> 17 public class MyMembershipprovider : MembershipProvider 18 { 19 20 public override void Initialize(string name, NameValueCollection config) 21 { 22 23 base.Initialize(name, config); 24 25 string author = (string)config["passwordFormat"]; 26 27 throw new Exception(author); 28 29 } 30 31 public MyMembershipprovider() 32 { 33 // 34 // TODO: Add constructor logic here 35 // 36 } 37 38 public override string ApplicationName 39 { 40 get 41 { 42 throw new Exception("The method or operation is not implemented."); 43 } 44 set 45 { 46 throw new Exception("The method or operation is not implemented."); 47 } 48 } 49 50 public override bool ChangePassword(string username, string oldPassword, string newPassword) 51 { 52 throw new Exception("The method or operation is not implemented."); 53 } 54 55 public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) 56 { 57 throw new Exception("The method or operation is not implemented."); 58 } 59 60 public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) 61 { 62 throw new Exception("The method or operation is not implemented."); 63 } 64 65 public override bool DeleteUser(string username, bool deleteAllRelatedData) 66 { 67 throw new Exception("The method or operation is not implemented."); 68 } 69 70 public override bool EnablePasswordReset 71 { 72 get { throw new Exception("The method or operation is not implemented."); } 73 } 74 75 public override bool EnablePasswordRetrieval 76 { 77 get { throw new Exception("The method or operation is not implemented."); } 78 } 79 80 public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) 81 { 82 throw new Exception("The method or operation is not implemented."); 83 } 84 85 public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) 86 { 87 throw new Exception("The method or operation is not implemented."); 88 } 89 90 public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) 91 { 92 throw new Exception("The method or operation is not implemented."); 93 } 94 95 public override int GetNumberOfUsersOnline() 96 { 97 throw new Exception("The method or operation is not implemented."); 98 } 99 100 public override string GetPassword(string username, string answer) 101 { 102 throw new Exception("The method or operation is not implemented."); 103 } 104 105 public override MembershipUser GetUser(string username, bool userIsOnline) 106 { 107 throw new Exception("The method or operation is not implemented."); 108 } 109 110 public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) 111 { 112 throw new Exception("The method or operation is not implemented."); 113 } 114 115 public override string GetUserNameByEmail(string email) 116 { 117 throw new Exception("The method or operation is not implemented."); 118 } 119 120 public override int MaxInvalidPasswordAttempts 121 { 122 get { throw new Exception("The method or operation is not implemented."); } 123 } 124 125 public override int MinRequiredNonAlphanumericCharacters 126 { 127 get { throw new Exception("The method or operation is not implemented."); } 128 } 129 130 public override int MinRequiredPasswordLength 131 { 132 get { throw new Exception("The method or operation is not implemented."); } 133 } 134 135 public override int PasswordAttemptWindow 136 { 137 get { throw new Exception("The method or operation is not implemented."); } 138 } 139 140 public override MembershipPasswordFormat PasswordFormat 141 { 142 get { throw new Exception("The method or operation is not implemented."); } 143 } 144 145 public override string PasswordStrengthRegularExpression 146 { 147 get { throw new Exception("The method or operation is not implemented."); } 148 } 149 150 public override bool RequiresQuestionAndAnswer 151 { 152 get { throw new Exception("The method or operation is not implemented."); } 153 } 154 155 public override bool RequiresUniqueEmail 156 { 157 get { throw new Exception("The method or operation is not implemented."); } 158 } 159 160 public override string ResetPassword(string username, string answer) 161 { 162 throw new Exception("The method or operation is not implemented."); 163 } 164 165 public override bool UnlockUser(string userName) 166 { 167 throw new Exception("The method or operation is not implemented."); 168 } 169 170 public override void UpdateUser(MembershipUser user) 171 { 172 throw new Exception("The method or operation is not implemented."); 173 } 174 175 176 public override bool ValidateUser(string username, string password) 177 { 178 bool returnValue = false; 179 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString)) 180 { 181 using (SqlCommand cmd = new SqlCommand()) 182 { 183 cmd.Connection = connection; 184 cmd.CommandType = CommandType.Text; 185 cmd.CommandText = "Select Count(*) From Kunden Where KundeName = @username AND Kundepasswort = @password"; 186 cmd.Parameters.AddWithValue("@username", username); 187 cmd.Parameters.AddWithValue("@password", password); 188 connection.Open(); 189 object result = cmd.ExecuteScalar(); 190 if (result != null) 191 { 192 if ((int)result == 1) 193 returnValue = true; 194 } 195 } 196 } 197 return returnValue; 198 } 199 200 } 201

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS