2

我正在使用 MVC 4 热毛巾模板,我现在已经解决了 MVC 方式,我有 _viewStart.cshtml:

@{
if (User.Identity.IsAuthenticated)
{
    Layout = "~/Views/Shared/_Layout.cshtml";
    Page.Title = "Home1";
}
else
{
    Layout = "~/Views/Shared/_loginLayout.cshtml";
    Page.Title = "Home2";
}}

在 index.cshtml 中:

@using System.Web
@using System.Web.Optimization
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

<script type="text/javascript">
        if (navigator.userAgent.match(/IEMobile\/10\.0/)) {
            var msViewportStyle = document.createElement("style");
            var mq = "@@-ms-viewport{width:auto!important}";
            msViewportStyle.appendChild(document.createTextNode(mq));
            document.getElementsByTagName("head")[0].appendChild(msViewportStyle);
        }
    </script>


    @if (@User.Identity.IsAuthenticated)
    {
    <div id="applicationHost">
        @Html.Partial("_splash")
    </div>

   @Scripts.Render("~/scripts/vendor");
        if(HttpContext.Current.IsDebuggingEnabled) {
            @Html.AntiForgeryToken()
            <script>
                window.userId = "@User.Identity.Name";
                console.log(window.userId);
            </script>
            <script type="text/javascript" src="~/App/durandal/amd/require.js" data-main="@Url.Content("~/App/main")"></script>

        } else {
            <!-- Remember to run the Durandal optimizer.exe to create the main-built.js  -->
            <script type="text/javascript" src="~/App/main-built.js"></script>
        }
    }
    else
    {
        <script src="~/Scripts/jquery-1.9.1.min.js"></script>

            <div id="login">
              <p>hello world</p>
            </div>
        }

我为登录创建了一个单独的视图模型用户:

define(['services/logger'], function (logger) {
    var vm = {
        activate: activate(),
        userName: ko.observable(),
        password: ko.observable()
    };

    return vm;

    //#region Internal Methods
    function activate() {
        logger.log('login View Activated', null, 'login', true);
        return true;
    }
    //#endregion
});

并创建了登录视图:

<section>
    <h2>My login model without content yet</h2>
</section>

(我知道我没有在这个视图中使用视图模型,但它仅用于测试)

我如何在 Durandal 中执行相同的功能?甚至有可能吗?

不讨厌,我是单页应用程序和 durandal + 微风.js + 淘汰赛的新手。

4

1 回答 1

0

我建议使用内置的 ASP.net 身份验证/登录机制 - 即<authentication>在 web.config 中。

我发现https://github.com/jamesc88/Durandal_Serverside_Authentication很有用。

于 2013-07-23T09:21:47.403 回答