开发者

Running MVC3 in an app folder/sub-domain gives me cascading URLs

I have a main website at http://sol3.net/. I have added an app directory under the main website at \caergalen and mapped it to a sub-domain at http://caergalen.sol3.net/. So far, everything seems to work ok - until I try to login.

NOTE: Logins work just fine when I run in dev as a stand alone app.

When I click the login button I am taken to http://caergalen.sol3.net/Account/LogOn. So far so good.

When I select my openid (and go through the login) I end up at http://caergalen.sol3.net/CaerGalen/Account/Register. (My account is not yet associated with the site). But notice - the name of my app directory is now part of the URL!


ADDENDUM:

My Controller:

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using CaerGalenMvc.Models;

namespace CaerGalenMvc.Controllers
{
    public class AccountController : Controller
    {
        private static OpenIdRelyingParty openid = new OpenIdRelyingParty();

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // **************************************
        // URL: /Account/LogOn
        // **************************************
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        // **************************************
        // URL: /Account/LogOff
        // **************************************
        public ActionResult LogOff()
        {
            FormsService.SignOut();

            return RedirectToAction("Index", "Home");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************
        public ActionResult Register()  //string OpenID)
        {
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            ViewBag.OpenID = Session["OpenID"].ToString();
            return View();
        }

        [HttpPost]
        public ActionResult Register(cgUser model)
        {
            model.OpenID = Session["OpenID"].ToString();
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Where(x => x.Value.Errors.Count > 0)
                                       .Select(x => new { x.Key, x.Value.Errors })
                                       .ToArray();
                int count = errors.Count();
            }

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                string PWD = string.Format("{0} {1}", model.MundaneFirstName, model.MundaneLastName);
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, PWD, model.Email, model.OpenID);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false);
                    Session["OpenID"] = null;
                    return RedirectToAction("Index", "Home", null);
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }

        // **************************************
        // URL: /Account/ChangePassword
        // **************************************
        [Authorize]
        public ActionResult ChangePassword()
        {
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }

        // **************************************
        // URL: /Account/ChangePasswordSuccess
        // **************************************
        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

        [ValidateInput(false)]
        public ActionResult Authenticate(string returnUrl)
        {
            var response = openid.GetResponse();
            if (response == null)
            {
                //Let us submit the request to OpenID provider
                Identifier id;
                if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
                {
                    try
                    {
                        var request = openid.CreateRequest(Request.Form["openid_identifier"]);
                        return request.RedirectingResponse.AsActionResult();
                    }
                    catch (ProtocolException ex)
                    {
                        ViewBag.Message = ex.Message;
                        return View("LogOn");
                    }
                }

                ViewBag.Message = "Invalid identifier";
                return View("LogOn");
            }

            //Let us check the response
            switch (response.Status)
            {

                case AuthenticationStatus.Authenticated:
                    LogOnModel lm = new LogOnModel();
                    lm.OpenID = response.ClaimedIdentifier;
                    // check if user exist
                    MembershipUser user = MembershipService.GetUser(lm.OpenID);
                    if (user != null)
                    {
                        lm.UserName = user.UserName;
                        FormsService.SignIn(user.UserName, false);
                        return RedirectToAction("Index", "Home");
                    }

                    // Need to register them now...
                    ViewBag.LogOn = lm;
                    ViewBag.PasswordLength = MembershipService.MinPasswordLength;
                    RegisterModel rm = new RegisterModel();

                    Session["OpenID"] = lm.OpenID;
                    return RedirectToAction("Register");

                case AuthenticationStatus.Canceled:
                    ViewBag.Message = "Canceled at provider";
                    return View("LogOn");
                case AuthenticationStatus.Failed:
                    ViewBag.Message = response.Exception.Message;
                    return View("LogOn");
            }

            return new EmptyResult();
        }
    }
}

My Logon view:

@model CaerGalenMvc.Models.LogOnModel
@{ ViewBag.Title = "Log On"; }

<form action="Authenticate?ReturnUrl=@HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"])" method="post" id="openid_form" class="openid">
<input type="hidden" name="action" value="verify" />
<fieldset class="ShowBorder">
    <legend>Login using OpenID</legend>
    <div>
        <ul class="providers">
            <li class="openid" title="OpenID">
                <img src="/content/images/openidW.png" alt="icon" />
                <span><strong>http://{your-openid-url}</strong></span></li>
            <li class="direct" title="Google">
                <img src="/content/images/googleW.png" alt="icon" /><span>https://www.google.com/accounts/o8/id</span></li>
            <li class="direct" title="Yahoo">
                <img src="/content/images/yahooW.png" alt="icon" /><span>http://yahoo.com/</span></li>
            <li class="username" title="AOL screen name">
                <img src="/content/images/aolW.png" alt="icon" /><span>http://openid.aol.com/<strong>username</strong></span></li>
            <li class="username" title="MyOpenID user name">
                <img src="/content/images/myopenid.png" alt="icon" /><span>http://<strong>username</strong>.myopenid.com/</span></li>
            <li class="username" title="Flickr user name">
                <img src="/content/images/flickr.png" alt="icon" /><span>http://flickr.com/<strong>username</strong>/</span></li>
            <li class="username" title="Technorati user name">
                <img src="/content/images/technorati.png" alt="icon" /><span>http://technorati.com/people/technorati/<strong>username</strong>/</span></li>
            <li class="username" title="Wordpress blog name">
                <img src="/content/images/wordpress.png" alt="icon" /><span>http://<strong>username</strong>.wordpress.com</span></li>
            <li class="username" title="Blogger blog name">
                <img src="/content/images/blogger.png" alt="icon" /><span>http://<strong>username</strong>.blogspot.com/</span></li>
            <li class="username" title="LiveJournal blog name">
                <img src="/content/images/livejournal.png" alt="icon" /><span>http://<strong>username</strong>.livejournal.com</span></li>
            <li class="username" title="ClaimID user name">
                <img src="/content/images/claimid.png" alt="icon" /><span>http://claimid.com/<strong>username</strong></span></li>
        开发者_JAVA百科    <li class="username" title="Vidoop user name">
                <img src="/content/images/vidoop.png" alt="icon" /><span>http://<strong>username</strong>.myvidoop.com/</span></li>
            <li class="username" title="Verisign user name">
                <img src="/content/images/verisign.png" alt="icon" /><span>http://<strong>username</strong>.pip.verisignlabs.com/</span></li>
        </ul>
    </div>
    <fieldset class="NoBorder">
        <label for="openid_username">
            Enter your <span>Provider user name</span></label>
        <div>
            <span></span>
            <input type="text" name="openid_username" /><span></span>
            <input type="submit" value="Login" /></div>
    </fieldset>
    <fieldset class="NoBorder">
        <label for="openid_identifier">
            Enter your <a class="openid_logo" href="http://openid.net">OpenID</a></label>
        <div>
            <input type="text" name="openid_identifier" />
            <input type="submit" value="Login" /></div>
    </fieldset>
    <noscript>
        <p>
            OpenID is service that allows you to log-on to many different websites using a single indentity. Find 
            out <a href="http://openid.net/what/">more about OpenID</a>and <a href="http://openid.net/get/">how to 
            get an OpenID enabled account</a>.</p>
    </noscript>
    <div>
            @if (Model != null)
            {
                // User does not exist...
                if (String.IsNullOrEmpty(Model.UserName))
                {
                <p>As this is your first time trying to login to this site you will need to complete our registration form.</p>
                <p class="button">
                    @Html.ActionLink("New User ,Register", "Register", new { OpenID = Model.OpenID })
                </p>
                }
                else
                {
                //user exists...
                <p class="buttonGreen">
                    <a href="@Url.Action("Index", "Home")">Welcome , @Model.UserName, Continue..." </a>
                </p>

                }
            }
        </div>
</fieldset>
</form>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.openid.js")" type="text/javascript"></script>
<script type="text/javascript">    $(function () { $("form.openid:eq(0)").openid(); });</script>

My Register view:

@model CaerGalenMvc.Models.cgUser
@{ ViewBag.Title = "Register"; }

<h2>Create a New Account</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    <div>
        <fieldset>
            <legend></legend>
            @Html.ValidationSummary(true, "Account creation was unsuccessful.  Please correct the errors and try again.")
            <p>
                Use the form below to create a new account.  Items with an <span style="color:red">**</span> are required.</p>
            @*<p>
                Passwords are required to be a minimum of @ViewBag.PasswordLength characters in length.</p>*@
            <p>
                All information collected here is for the express reason of making your visit to this site personalized.  Mundane 
                information collected will not be sold or released without your written (or electronic signature) permission.</p>
            <p>
                This site will have several contact pages where someone can come to the page and select you (by Society name only) 
                as a recipient and this site will send you an email.  The person originating the contact will not be getting an 
                email.  After you receive the email it is up to you to respond or not as you feel fit and/or comfortable doing
                so.</p>
            <hr />
            <br />
            <table>
                <tr>
                    <td colspan="2"><h3>Account Information</h3></td>
                </tr>
                @if (ViewBag.OpenID != null || ViewData["OpenID"] != null)
                {
                <tr>
                    <td class="editor-label">@Html.Label("OpenID"):</td>
                    <td class="editor-field">
                        You have been successfully authenticated by your login provider!
                    </td>
                </tr>
                }
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.Email):</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.Email) <span style="color:red">**</span> @Html.ValidationMessageFor(m => m.Email)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.UserName):</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.UserName) <span style="color:red">**</span> @Html.ValidationMessageFor(m => m.UserName)</td>
                </tr>
                <tr>
                    <td colspan="2"><br /><h3>Society Information</h3></td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.SocietyTitle):</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.SocietyTitle)@Html.ValidationMessageFor(m => m.SocietyTitle)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.SocietyFirstName):</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.SocietyFirstName)@Html.ValidationMessageFor(m => m.SocietyFirstName)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.SocietyMiddleName):</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.SocietyMiddleName)@Html.ValidationMessageFor(m => m.SocietyMiddleName)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.SocietyLastName):</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.SocietyLastName)@Html.ValidationMessageFor(m => m.SocietyLastName)</td>
                </tr>
                <tr>
                    <td colspan="2"><br /><h3>Mundane Information</h3></td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.MundaneFirstName)</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.MundaneFirstName) <span style="color:red">**</span> @Html.ValidationMessageFor(m => m.MundaneFirstName)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.MundaneMiddleName)</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.MundaneMiddleName)@Html.ValidationMessageFor(m => m.MundaneMiddleName)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.MundaneLastName)</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.MundaneLastName) <span style="color:red">**</span> @Html.ValidationMessageFor(m => m.MundaneLastName)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.PhoneNumberPrimary)</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.PhoneNumberPrimary)@Html.ValidationMessageFor(m => m.PhoneNumberPrimary)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.PhoneNumberSecondary)</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.PhoneNumberSecondary)@Html.ValidationMessageFor(m => m.PhoneNumberSecondary)</td>
                </tr>
                <tr>
                    <td class="editor-label">@Html.LabelFor(m => m.PhoneContactInfo)</td>
                    <td class="editor-field">@Html.TextBoxFor(m => m.PhoneContactInfo)@Html.ValidationMessageFor(m => m.PhoneContactInfo)</td>
                </tr>
                <tr>
                    <td class="editor-label">&nbsp;</td>
                    <td class="editor-field"><input type="submit" value=" Register " /></td>
                </tr>
            </table>
        </fieldset>
    </div>
}

I then fill in the required fields and click "Register" and I end up with an error. Something is just not quite working right and I am not clever enough to go "Ah ha! That is the wrong thing."

Any one with any insights?


It looks like an issue on how you are setting up the redirect link in your application. This is most likely a view issue, so I would look at the values in the view first. If necessary spit out the value of the redirect link as a temporary "throw it to the browser" option.

If the link is set up in the controller (view model altered in controller), etc., you can set breakpoints and see what is going on.

Once you have determined where the problem is, you will have enough information to tackle the monkey.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜