Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem setting up membership and authentication providers 2

Status
Not open for further replies.

AndyH1

Programmer
Jan 11, 2004
350
GB
I'm trying to set up an intranet site and wanted to use the ASP.NET membership and role management providers. I want however to use my local MS SQL Server 2005 rather than an .mdf file stored on the site. I'm running win 2003 server with asp.net 3.5 and MS SQL 2005.

Iv'e created the aspnetdb database using
aspnet_regsql.exe –A m –E
and this seems ok and has been created as a database within MS SQL 2005 with all the tables

and added the following to my web.config file

<connectionStrings>
<!--remove name="LocalSqlServer"/-->
<add name="LocalSql2005Server" connectionString="data source=127.0.0.1;Integrated Security=SSPI" />
</connectionStrings>
<system.web>
<membership defaultProvider="AspNetSql2005MembershipProvider">
<providers>
<add name="AspNetSql2005MembershipProvider"
type="System.Web.Security.SqlMembershipProvider,
System.Web, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="LocalSql2005Server"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
applicationName="/"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="7"
minRequiredNonalphanumericCharacters="1"
passwordAttemptWindow="10"
passwordStrengthRegularExpression="" />
</providers>
</membership>

<roleManager enabled="true" cacheRolesInCookie="true"
cookieName=".ASPXROLES" cookieTimeout="30" cookiePath="/"
cookieRequireSSL="false" cookieSlidingExpiration="true"
cookieProtection="All" defaultProvider="AspNetSqlRoleProvider"
createPersistentCookie="false" maxCachedResults="25">
<providers>
<clear />
<add connectionStringName="LocalSql2005Server" applicationName="/"
name="AspNetSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider, System.Web,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>

<profile>
<providers>
<clear />
<add name="AspNetSql2005ProfileProvider"
connectionStringName="LocalSql2005Server" applicationName="/"
type="System.Web.Profile.SqlProfileProvider, System.Web,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
<properties>
<add name="FirstName" />
<add name="LastName" />
<add name="LastVisited" />
<add name="Age" />
<add name="Member" />
</properties>
</profile>

and also included:

<authentication mode="Forms">
<forms name=".ASPXAUTH"
loginUrl="Login.aspx"
protection="All"
timeout="30"
path="/"
requireSSL="false"
slidingExpiration="true"
cookieless="UseDeviceProfile" />
</authentication>

Then tried to create a form page:

<form id="form1" runat="server">
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server"
BorderWidth="1px" BorderColor="#FFDFAD" BorderStyle="Solid"
BackColor="#FFFBD6" Font-Names="Verdana">
<TitleTextStyle Font-Bold="True" BackColor="#990000"
ForeColor="White"></TitleTextStyle>
</asp:CreateUserWizard>
</form>

when I try running this and enter valid data though I get:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.

Instead of the

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I tried creating a user called poweruser (with password set to secret) in MS SQL Server and giving it db owner access to the aspnetdb databsse and then tried the following connection:

<add name="LocalSql2005Server" connectionString="data source=ANDYS-PC\ANDYS-PC,1433;Integrated Security=SSPI;Initial Catalog=aspnetdb;User ID=poweruser;Password=secret" />

My SQL server is called 'ANDYS-PC' as well as the computers name. But this still gives the same error. I also tried combinations using 12.0.0.1 instead of PC\SERVER but still get the same error.

Can anyone advise what I'm doing wrong or have missed. I'm pretty new to setting up security and roles on a site so please bear with me and any help would be appreciated

Thanks
Andy




 
Code:
<add name="LocalSql2005Server" connectionString="data source=ANDYS-PC\ANDYS-PC,1433;[!]Integrated Security=SSPI;[/!]Initial Catalog=aspnetdb;User ID=poweruser;Password=secret" />
You are still telling the database to connect using your windows credentials so it is connecting as the NETWORK SERVICE account which doesn't have access. Remove that bit and you should be good to go.

Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
The create user seemed to work fine but I'm having problems with the logon control not redirecting. can anyone advise?

I put
<authentication mode="Forms">
<forms name=".ASPXAUTH"
loginUrl="Login.aspx"
protection="All"
timeout="30"
path="/"
requireSSL="false"
slidingExpiration="true"
cookieless="UseDeviceProfile" />
</authentication>
<authorization>
<deny users="*" />
</authorization>

in the web.config

When I then try to open default.aspx it redirects, as it should, to the Login.aspx page with


On the login page I have

<form id="form1" runat="server">
<asp:Login ID="Login1" Runat="server">
</asp:Login>
</form>

and have also tried

<asp:Login ID="Login1" Runat="server"
DestinationPageUrl="~/Default.aspx">
<TitleTextStyle Font-Bold="True" BackColor="#6B696B"
ForeColor="#FFFFFF"></TitleTextStyle>
</asp:Login>

which show correctly, but neither of these redirects if I enter the correct username and password. It just reshows the logon screen. However if I enter an incorrect username or password I do get 'Your login attempt was not successful. Please try again' so it must be authenticating correctly. Am I missing something, I thought the logon control dealt with the redirect and theres no need to write code?

Thanks
Andy
 
I think you need the RedirectFromLogin attribute in the web.config.
 
Thanks - but I haven't been able to find any reference about where I put this attribute in the web.config or what its set too. Could you advise?
Thanks
Andy
 
Sorry, my mistake. It does not go in the web.config, it goes in the code behind:
Code:
 Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs)
        If (FormsAuthentication.Authenticate(txtUserName.Text, txtPassword.Text)) Then
			lblError.Text="Arg!-"' & FormsAuthentication.GetRedirectUrl(txtUserName.Text, chkRememberMe.Checked)
            FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkRememberMe.Checked)
        Else
            lblError.Text = "Invalid user name/password"
        End If
    End Sub
 
Thanks jbenson001

I had seen the code you mention in a couple of references, but I couldn't figure out how to use it because of the txtUsername, txtPassword, chkRememberMe (VS says these are not declared). The code produced by dragging the login control on is:

<form id="form1" runat="server">
<asp:Login ID="Login1" Runat="server">
</asp:Login>
</form>

doesn't show the txtboxes or tickbox. Is there some way of setting the IDs of these. Or can you get these from the parameters passed?
Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

Thanks
Andy
 
Sorry its ok found I can access them with: Login1... ie

If (FormsAuthentication.Authenticate(Login1.UserName, Login1.Password)) Then
lblError.Text = FormsAuthentication.GetRedirectUrl(Login1.UserName, Login1.RememberMeSet)
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet)
Else
lblError.Text = "Invalid user name/password"
End If
End Sub

Will see if it works
Andy
 
Nope still having problems. In this case it seems to reject everything. I created a new user with the asp configuration tool to make sure I hadn't got may password wrong - used an easy one Fred and bubbles but it gets rejected.

If I go back to the old code though I only get an error message if I enter an actual invalid password or username, if I enter Fred and bubbles it seems just goes back to the same screen but without an error message.

Kind of a bit stuck now:-(
AndyH1
 
jbenson001

Doh - figured it out. I had:

<authorization>
<deny users="*" />
</authorization>

(ie deny all users)

instead of

<authorization>
<deny users="?" />
</authorization>

(ie deny anonymous users)

in my web.config. Sorry to have wasted you time, but thanks for all your help - much appreciated
AndyH1

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top