在WEB API中设置表单身份验证

时间:2017-06-09 05:18:37

标签: javascript jquery asp.net-mvc asp.net-web-api form-authentication

其实我有两个项目。一个用于Mvc,另一个用于Web Api。我在mvc项目中写了如下表格。

 <form>
            <div class="form-group">
                <input type="email" class="form-control" id="email" placeholder="Username">
            </div>
            <div class="form-group">
                <input type="password" class="form-control" id="pwd" placeholder="Password">
            </div>
            <div class="checkbox">
                <input class="customCheckBox" onclick="setIsRemember()" type="checkbox" name="" value="false"><label for=""><span><span></span></span>Remember me</label>
            </div>
            <button id="buttonSubmit" class="btn btn-default">LOG IN</button>
</form>

然后我为跨域写了脚本,如下所示,

 $("#buttonSubmit").click(function (e) {
            var user =
                {
                    UserName:  $("#email").val(),
                    Password: $("#pwd").val(),
                    IsRemember: $(".customCheckBox").val() 
                }
            $.ajax({
                type: "POST",
                url: "http://localhost:55016/api/ajaxapi/loginmethod",
                data: user,
                success: function (response) {
                    if (response.Success == false) {
                        alert("login fail");
                    }
                    if (response.Success == true) {
                      alert("login true");
                    }
                }
            });
 });

我已在网络Api中编写了登录凭据检查。登录成功后,我在web api源中设置了表单身份验证,

public class UserLogOn
    {
        public interface IFormsAuthentication
        {
            void SignIn(string userName, bool createPersistentCookie);

            void SignOut();
        }

        public IFormsAuthentication FormsAuth
        {
            get;
            private set;
        }
public void FormsChange(IFormsAuthentication formsAuth)
        {
            this.FormsAuth = formsAuth ?? new FormsAuthenticationService();
        }

        public void LogOnUserCookieCreation(UserValuesForLogOn user)
        {
            this.FormsChange(null);
            this.FormsAuth.SignIn(user.UserName, user.IsRemember);
            if (user.IsRemember)
            {
                HttpCookie cookie = new HttpCookie("SignedIn");
                cookie.Values.Add("UserName", user.UserName);
                cookie.Values.Add("Password", user.Password);
                FormsAuthentication.SetAuthCookie(user.UserName, true);
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
 public class FormsAuthenticationService : IFormsAuthentication
        {
            public void SignIn(string userName, bool createPersistentCookie)
            {
                FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
            }

            public void SignOut()
            {
                FormsAuthentication.SignOut();
            }
        }

如果在同一个项目中设置身份验证,则以上代码正常工作。但是使用webapi设置身份验证时它无法正常工作。请分享您的建议。 谢谢......

0 个答案:

没有答案