asp .net web api和backbone.js [英] asp .net web api and backbone.js

查看:63
本文介绍了asp .net web api和backbone.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i我正在创建一个门户网站,其中有三个角色供用户使用,例如1用于教师,1用于学生,另一个用于用户(正常)。我正在使用html5,jquery,javascript for interface,backbone.js和web api.i想为所有用户创建登录服务,当用户注册数据将保存在 sql数据库中并且也想要创建新令牌,当用户登录时,他应该获得令牌以返回dat将用于该门户中的每个请求,其具有用户信息,例如将在某个请求中使用的用户信息和其他信息。这个服务想要在 web api 中创建并与 backbone.js一起使用。



所以我能走上正轨可以做到这一点。如果是的话怎么样?我非常需要你的帮助..



请不要理睬我糟糕的语言..





先谢谢..

解决方案

你想知道这项技术吗?基本上对于客户端来说,backbone.js非常漂亮。如果你想使用web api那么它取决于你的要求。



现在进行身份验证过程,如果你使用表单身份验证,那么你可以创建一个票证来设置它cookie。也可以将用户标识( GenericIdentity )存储到 HttpContext.Current.User ,以便从应用程序的任何位置获取登录用户的信息。

  public   static   void  SetAuthInfo(Guid Id,字符串电子邮件,字符串 FullName,  string 角色,Guid PersonId,Guid OrganizationId 
字符串 exchangeUID, string exchangePWD, string exchangeDomain, int ?fileManagerPermission)
{
if (HttpContext.Current.Response.Cookies [FormsAuthentication.FormsCookieName]!= null
{
HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);

}

string fullName = FullName;

var ticket = new FormsAuthenticationTicket(
1
fullName,
DateTime.Now,
DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
false
string .Format( {0} _ {1} _ {2} _ {3} _ {4} _ {5} _ {6} _ {7} _ {8} _ {9} ,Email,Id,FullName,Role,PersonId,OrganizationId,exchangeUID,
July.Common.Encryption.Encrypt(exchangePWD),exchangeDomain,fileManagerPermission == null 0 :fileManagerPermission));

var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket)
{
HttpOnly = true
Secure = FormsAuthentication.RequireSSL
};
var ASPSESSID = new HttpCookie( ASPSESSID
{
HttpOnly = true
Secure = FormsAuthentication.RequireSSL,
Expires = DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes)
};


var identity = new GenericIdentity(ticket.Name) ;
var principal = new CustomUserPrincipal(identity,Id,Email,Role,FullName,PersonId, OrganizationId,exchangeUID,exchangePWD,exchangeDomain);
HttpContext.Current.User = principal;
}





上述代码将用于在登录后设置身份验证信息。在web.config文件中,您需要添加以下节点。



 <  身份验证    mode   = 表单 >  
< 表格 defaultUrl = 〜/ Home / Index timeout = 30 名称 = FormsAuthentication loginUrl = 〜/ Account / Login / >
< span class =code-keyword>< / authentication >
< 授权 >
< deny 用户 = / >
< / authorization >





然后创建一个登录页面。验证,如果验证,则通过上面的SetAuthInfo存储数据。


hi everyone,

i am creating a portal in which three roles are there for user like 1 is for teachers, 1 is for students, and the other is for users(normal). i am using html5,jquery,javascript for interface, backbone.js and web api.i wanted to create login service for all users in that when user register data will be saved in sql database and also want to create new token and when user login he should get the token in return dat will be used for every request in that portal which has users information like userid and other info which will be used in some request. this service want to create in web api and use it with backbone.js.

so am i on right track can this be possible. if yes than how?? i am badly needed your help..

kindly please ignore my poor language..


Thanks in Advance..

解决方案

did you want to know the technology ? basically for client side, backbone.js is very beautiful. if you want to use web api then it depends on your requirement.

Now for authentication process, if you use forms authentication then you can create a ticket to set it to cookie.Also you can store the user identification( GenericIdentity ) to HttpContext.Current.User also to get the logged in user's information from any where of the application.


public static void SetAuthInfo(Guid Id, string Email, string FullName, string Role, Guid PersonId, Guid OrganizationId
            , string exchangeUID, string exchangePWD, string exchangeDomain,int? fileManagerPermission)
        {
            if (HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                
            }

            string fullName = FullName;

            var ticket = new FormsAuthenticationTicket(
                1,
                fullName,
                DateTime.Now,
                DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
                false,
                string.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_{9}", Email, Id, FullName, Role, PersonId, OrganizationId, exchangeUID,
                July.Common.Encryption.Encrypt(exchangePWD), exchangeDomain, fileManagerPermission==null?0:fileManagerPermission));

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL
            };
            var ASPSESSID = new HttpCookie("ASPSESSID")
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL,
                Expires = DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes)
            };


            var identity = new GenericIdentity(ticket.Name);
            var principal = new CustomUserPrincipal(identity, Id, Email, Role, FullName, PersonId, OrganizationId, exchangeUID, exchangePWD, exchangeDomain);
            HttpContext.Current.User = principal;
}



The above code will be used to set the authentication info after login. In web.config file, you need to add the following nodes.

<authentication mode="Forms">
    <forms defaultUrl="~/Home/Index" timeout="30" name="FormsAuthentication" loginUrl="~/Account/Login" />
  </authentication>
  <authorization>
    <deny users="?" />
  </authorization>



Then create a page for login. validate in and if validate then store the data by above SetAuthInfo.


这篇关于asp .net web api和backbone.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆