0

I have generated an ASP.NET C# website with the visual studio 2013 wizard in order to get the authentication code and add it to my web app.

I have copied all files and everything looks fine except one thing...

the RegisterExternalLogin.cs file gives me an error in my web, but in the generated sample it works and I don't know why.

This is the error:

RegisterExternalLogin.aspx.cs(65,17,65,25): error CS0103: The name 'userName' does not exist in the current context
RegisterExternalLogin.aspx.cs(82,55,82,63): error CS0103: The name 'userName' does not exist in the current context

Here is the RegisterExternalLogin.aspx

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="RegisterExternalLogin.aspx.cs" Inherits="Account_RegisterExternalLogin" Async="true" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <h3>Register with your <%: ProviderName %> account</h3>

    <asp:PlaceHolder runat="server">
        <div class="form-horizontal">
            <h4>Association Form</h4>
            <hr />
            <asp:ValidationSummary runat="server" ShowModelStateErrors="true" CssClass="text-danger" />
            <p class="text-info">
                You've authenticated with <strong><%: ProviderName %></strong>. Please enter a user name below for the current site
                and click the Log in button.
            </p>

            <div class="form-group">
                <asp:Label runat="server" AssociatedControlID="userName" CssClass="col-md-2 control-label">User name</asp:Label>
                <div class="col-md-10">
                    <asp:TextBox runat="server" ID="userName" CssClass="form-control" />
                    <asp:RequiredFieldValidator runat="server" ControlToValidate="userName"
                        Display="Dynamic" CssClass="text-danger" ErrorMessage="User name is required" />
                    <asp:ModelErrorMessage runat="server" ModelStateKey="UserName" CssClass="text-danger" />
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <asp:Button runat="server" Text="Log in" CssClass="btn btn-default" OnClick="LogIn_Click" />
                </div>
            </div>
        </div>
    </asp:PlaceHolder>
</asp:Content>

and RegisterExternalLogin.aspx.cs

using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System;
using System.Web;
using QuoteHD;

public partial class Account_RegisterExternalLogin : System.Web.UI.Page
{
    protected string ProviderName
    {
        get { return (string)ViewState["ProviderName"] ?? String.Empty; }
        private set { ViewState["ProviderName"] = value; }
    }

    protected string ProviderAccountKey
    {
        get { return (string)ViewState["ProviderAccountKey"] ?? String.Empty; }
        private set { ViewState["ProviderAccountKey"] = value; }
    }

    protected void Page_Load()
    {
        // Process the result from an auth provider in the request
        ProviderName = IdentityHelper.GetProviderNameFromRequest(Request);
        if (String.IsNullOrEmpty(ProviderName))
        {
            Response.Redirect("~/Account/Login");
        }
        if (!IsPostBack)
        {
            var manager = new UserManager();
            var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
            if (loginInfo == null)
            {
                Response.Redirect("~/Account/Login");
            }
            var user = manager.Find(loginInfo.Login);
            if (user != null)
            {
                IdentityHelper.SignIn(manager, user, isPersistent: false);
                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else if (User.Identity.IsAuthenticated)
            {
                // Apply Xsrf check when linking
                var verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId());
                if (verifiedloginInfo == null)
                {
                    Response.Redirect("~/Account/Login");
                }

                var result = manager.AddLogin(User.Identity.GetUserId(), verifiedloginInfo.Login);
                if (result.Succeeded)
                {
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    AddErrors(result);
                    return;
                }
            }
            else
            {
                userName.Text = loginInfo.DefaultUserName;
            }
        }
    }

    protected void LogIn_Click(object sender, EventArgs e)
    {
        CreateAndLoginUser();
    }

    private void CreateAndLoginUser()
    {
        if (!IsValid)
        {
            return;
        }
        var manager = new UserManager();
        var user = new ApplicationUser() { UserName = userName.Text };
        IdentityResult result = manager.Create(user);
        if (result.Succeeded)
        {
            var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
            if (loginInfo == null)
            {
                Response.Redirect("~/Account/Login");
                return;
            }
            result = manager.AddLogin(user.Id, loginInfo.Login);
            if (result.Succeeded)
            {
                IdentityHelper.SignIn(manager, user, isPersistent: false);
                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                return;
            }
        }
        AddErrors(result);
    }

    private void AddErrors(IdentityResult result)
    {
        foreach (var error in result.Errors)
        {
            ModelState.AddModelError("", error);
        }
    }
}
mason
  • 31,774
  • 10
  • 77
  • 121
Mario
  • 13,941
  • 20
  • 54
  • 110
  • You have marked this with model-view-controller... is this a WebForms site or an MVC site? the tag model-view-controller is related to the mvc pattern, not asp.net MVC. – Erik Funkenbusch Aug 01 '14 at 18:53
  • It's a webforms, but the new visual studio 2013 allows combined websites with mvc and webforms... – Mario Aug 01 '14 at 19:13
  • Look like there was failure in reading. Lemme elaborat. **"MVC" is a name of language-independent architectural pattern, which defines constraints for information flow between application's layers.** It is **NOT** a name of a framework. The framework from Microsoft, which you are refering to is called "ASP.NET MVC" and it has **NOTHING** to do with previously mentioned patter. Instead it is an adaptation of Rails-like architecture for .NET code. – tereško Aug 01 '14 at 19:23
  • Ok, but my application is a webforms app and the wizard generated app is the same, not mvc. This is how VS2013 generates the apps. I needed for the authentication code. – Mario Aug 01 '14 at 19:27
  • Also, as you can see here, mvc can be combined with webforms... http://stackoverflow.com/questions/2203411/combine-asp-net-mvc-with-webforms – Mario Aug 01 '14 at 19:41
  • @MarioM Although you can combine MVC and Web Forms and Web API into a single project, that doesn't mean you should include the tags if it's not related to your question. Seeing as your question has nothing to do with ASP.NET MVC, I have removed the tag. – mason Aug 01 '14 at 20:41

1 Answers1

0

I solved the problem...

I had to wrap the entire codebehind in a namespace

QuoteHD.Account
{
}
Mario
  • 13,941
  • 20
  • 54
  • 110